PDA

View Full Version : [Resolved] Array Sorting Question




larswik
Sep 3, 2012, 08:02 PM
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.

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?



MattInOz
Sep 3, 2012, 08:29 PM
- (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.

larswik
Sep 3, 2012, 08:56 PM
Problem solved, thank you. First time playing with NSSortDescriptor.