I have an NSMutableArray with 70 objects, they are NSMutableDictionary, inside each dictionary I have 2 values: “distance” and “indexPath”. I want to sort the NSMutableArray from the small distance to the biggest. The distance is in KM and the array looks like this after using the code that I pasted here:
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
[branchDistance sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
This is the result of this method:
(lldb) po [branchDistance objectAtIndex:0]
(id) $1 = 0x0d6ac240 {
"<NSIndexPath 0xd6cf180> 1 indexes [0]" = indexPath;
"8.078547" = distance;
}
(lldb) po [branchDistance objectAtIndex:1]
(id) $2 = 0x0d6a64a0 {
"<NSIndexPath 0xd6cf3b0> 1 indexes [1]" = indexPath;
"45.044069" = distance;
}
(lldb) po [bran(lldb) po [branchDistance objectAtIndex:2]
(id) $4 = 0x0d6cf550 {
"<NSIndexPath 0xd69b3f0> 1 indexes [2]" = indexPath;
"9.992081" = distance;
}
(lldb) po [branchDistance objectAtIndex:3]
(id) $5 = 0x0d6cf750 {
"283.356727" = distance;
"<NSIndexPath 0xd6cf480> 1 indexes [3]" = indexPath;
}
I want to sort the NSMutableArray from the small number to the big number, using this “distance” key value inside the NSMutableDictionary. I tried few pieces of code from the web and from stackoverflow, but couldn’t find something that worked for me.
Please help me!
Thanks!
I assume the distance values are stored in the dictionaries using an
NSNumberobject: