0

Possible Duplicate:
How to sort NSMutableArray using sortedArrayUsingDescriptors?

I'm confused how I would go about sorting this. I don't entirely understand NSDictionary objects but I did some searching and a lot of people recommended using them to maintain original indexs of array elements before sorting. If there is a better way to do it, please let me know, but for right now I am adding NSDicitonary objects (with a NSNumber value + NSNumber key) to an NSMutableArray. I then want to sort the array based on the NSNumber values. This is how I have it set up right now:

NSMutableArray *sortindx = [[[NSMutableArray alloc] initWithCapactiy: M] autorelease];

for(int i = 0; i < M; i++) {
    //Each element is  a single number value with a key of its original array index
    NSDictionary *anElement=[NSDictionary dictionaryWithObject:[NSNumber numberWithInt: numbers[i]] forKey:[NSNumber numberWithInt:i]];
    [sortindx addObject: anElement];
}

How would I sort "sortindx" based on the values of the dictionaries without altering their keys?

My ultimate goal is something like this:

{100, 0}, {20, 1}, {50, 2} //before sort
{20, 1}, {50, 2},  {100,0} //after sort
Community
  • 1
  • 1
MrHappyAsthma
  • 6,332
  • 9
  • 48
  • 78

0 Answers0