Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I have an NSMutableArray with NSDictionary's in them. Each NSDictionary has an Key/Value of combatDate = 1234 for example. This value is the seconds since the game was started. The value in the dict is an NSNumber.
2012-09-03 17:42:21.366 XPm Tracker[3753:10703] tempArray(
{
DOA = NO;
combatDate = 387;
combatNumber = 4;
combatTable = (
"3 - XP: 3"
);
level = 0;
name = "5 : gght";
notes = "Notes:";
xp = 3;
},
{
DOA = NO;
combatDate = 494;
combatNumber = 5;
combatTable = (
"56 - XP: 56"
);
level = 0;
name = "5 : yyt";
notes = "Notes:";
xp = 56;
},

I am trying to sort the NSMutableArray by the different values in the NSDictionary 'combatDate' from lowest to highest value.

I am using this sort code which is not working.
Code:
    NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"combatDate"  ascending:YES];
    [tempArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
    newArray = [tempArray copy];

But when I do an NSLog to check the order of the new array it is not correct, here is the full list

2012-09-03 17:42:21.367 XPm Tracker[3753:10703] NewArray(
{
DOA = NO;
combatDate = 387;
combatNumber = 5;
combatTable = (
"3 - XP: 3"
);
level = 0;
name = "5 : gght";
notes = "Notes:";
xp = 3;
},
{
DOA = NO;
combatDate = 494;
combatNumber = 5;
combatTable = (
"56 - XP: 56"
);
level = 0;
name = "5 : yyt";
notes = "Notes:";
xp = 56;
},
{
DOA = NO;
combatDate = 380;
combatNumber = 4;
combatTable = (
"47 - XP: 47"
);
level = 0;
name = "4 : ert";
notes = "Notes:";
xp = 47;
},
{
DOA = NO;
combatDate = 234;
combatNumber = 1;
combatTable = (
"8 - XP: 8"
);
level = 0;
name = "1 : iu";
notes = "Combat Notes:";
xp = 8;
},
{
DOA = NO;
combatDate = 507;
combatNumber = 6;
combatTable = (
"85 - XP: 85"
);
level = 0;
name = "6 : zxs";
notes = "Notes:";
xp = 85;
}
)

What am I missing?
 
Last edited:
Code:
- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors

Your not capturing the results of the sort into an NSArray object.
So newArray is a copy of the unsorted array.
 
Last edited:
Problem solved, thank you. First time playing with NSSortDescriptor.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.