1

It's possible to sort an array like this:

MutableArray  = [NSMutableArray arrayWithObjects:@"5",@"1",@"7,@"9",@"4", nil];

to (start big ends small):

MutableArray  = [NSMutableArray arrayWithObjects:@"9",@"7",@"5,@"4",@"1", nil];

Is it also possible to sort two arrays:

MutableArray  = [NSMutableArray arrayWithObjects:@"5",@"1",@"7",@"9",@"4", nil];
MutableArray  = [NSMutableArray arrayWithObjects:@"Andy",@"Mike",@"Bob",@"Amy",@"Alex", nil];

to (from big to small, but Andy got 5 point, Mike got 1 and so on):

MutableArray  = [NSMutableArray arrayWithObjects:@"9",@"7",@"5",@"4",@"1", nil];
MutableArray  = [NSMutableArray arrayWithObjects:@"Amy",@"Bob",@"Andy",@"Alex",@"Mike", nil];

Is it possible to order them as a couple?
Thanks in Advance :)

4
  • 1
    you'd better to take a look at nsdictionary, it seems... Commented Aug 10, 2012 at 16:32
  • 6
    Yes its called sorting Commented Aug 10, 2012 at 16:32
  • Because an NSDictionary can hold keys (players) and values (scores). So when you sort one, you automatically sort both. Commented Aug 10, 2012 at 16:38
  • If you don't want a dictionary, objects would work too. (Anything's better than matching parallel arrays.) Commented Aug 10, 2012 at 16:45

2 Answers 2

2

Example how to sort arrayOne ascending and sort arrayTwo along:

NSMutableArray *arrayOne  = [NSMutableArray arrayWithObjects:@"5",@"1",@"7",@"9",@"4", nil];
NSMutableArray *arrayTwo = [NSMutableArray arrayWithObjects:@"Andy",@"Mike",@"Bob",@"Amy",@"Alex", nil];

NSDictionary *temp = [NSDictionary dictionaryWithObjects:arrayTwo forKeys:arrayOne];
NSArray *sortedArrayOne = [[temp allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSArray *sortedArrayTwo = [temp objectsForKeys:sortedArrayOne notFoundMarker:[NSNull null]];

NSLog(@"%@",sortedArrayOne);
NSLog(@"%@",sortedArrayTwo);

Workflow:
Create dictionary from the arrays -> sort dictionary -> create arrays from dictionary.

EDIT

Use NSSortDescriptor with ascending:NO to sort descending, quick example:

NSDictionary *temp = [NSDictionary dictionaryWithObjects:arrayTwo forKeys:arrayOne];
NSSortDescriptor *theDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(compare:)];    
NSArray *sortedArrayOne = [[temp allKeys] sortedArrayUsingDescriptors:[NSArray arrayWithObject:theDescriptor]];
NSArray *sortedArrayTwo = [temp objectsForKeys:sortedArrayOne notFoundMarker:[NSNull null]];
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks :) works great :) but is it possible to do the opposite? instead of 1,4,5,7,9 then get it to 9,7,5,4,1 (with names)?
Added quick example how to sort descending.
if i use the number 22, it'll be places between 1 and 4.. how to change that?
Replace @selector(compare:) by @selector(localizedStandardCompare:).
Sry for all this.. but: if i use the number 22, it'll be places between 1 and 4.. how can I change that??
|
0

Why don't you just create some new object let's say Player with ivars name and score and store instances of that class in array. Then you'll be able to sort that array as you want for any point in your code like so:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] ;
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedByName = [players sortedArrayUsingDescriptors:sortDescriptors];

or by score:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"score" ascending:NO] ;
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedByName = [players sortedArrayUsingDescriptors:sortDescriptors];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.