Alex
Alex

Reputation: 11147

setting multiple sort descriptors

I have a array of objects and need to sort it by rating and after by number of votes descendent(so that if two 5 stars rated elements compare, the one with the most votes should be first)

Is it possible to sort a NSArray by two descriptors:first after rating and then after votes count?

I've found on http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html

something like sortUsingDescriptors: but i can't find it anywhere in the docs, think it's deprecated.

Upvotes: 11

Views: 7305

Answers (5)

SimpleojbC
SimpleojbC

Reputation: 127

If you have one NSArray use this one:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"x_date" ascending:TRUE];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"x_time" ascending:TRUE];
[tempArray sortUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor,sortDescriptor1, nil]];

Upvotes: 0

Phil
Phil

Reputation: 121

Here's a one-liner.

NSArray *sortedArray = [unsortedArray sortUsingDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey@"rating" ascending:NO], [NSSortDescriptor sortDescriptorWithKey@"date" ascending:NO], nil]];

Upvotes: 1

Fran Sevillano
Fran Sevillano

Reputation: 8163

Use - (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors. Here is the documentation: NSArray Class Reference

E.g:

NSSortDescriptor *sortRating = nil;
NSSortDescriptor *sortDate = nil;

NSSortDescriptor *sortRating = [[NSSortDescriptor alloc] initWithKey:@"rating" ascending:NO];
NSSortDescriptor *sortDate = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *sortedArray = [auxArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortRating,sortDate,nil]];

[sortRating release];
sortRating = nil;

[sortDate release];
sortDate = nil;

Cheers!

Upvotes: 0

rckoenes
rckoenes

Reputation: 69499

Yes you can:

NSSortDescriptor *sortRating = [[NSSortDescriptor alloc] initWithKey:@"rating" ascending:NO];
NSSortDescriptor *sortVotes = [[NSSortDescriptor alloc] initWithKey:@"votes" ascending:NO];

NSArray *sortedArray = [orignalAray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortRating, sortVotes, nil]];
[sortRating release], sortRating = nil;
[sortVotes release], sortVotes = nil;

Upvotes: 30

Matthew Gillingham
Matthew Gillingham

Reputation: 3429

You are basically right. On NSArray there is the

- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors 

method. This will return a new array, sorted according to various descriptors.

- (void)sortUsingDescriptors:(NSArray *)sortDescriptors

does exist, but is on NSMutableArray, which might explain why you couldn't find it in the documentation for NSArray. It accomplishes the same goal, but sorts the array you call it on, rather than returning a new array. Neither is deprecated.

Upvotes: 1

Related Questions