Lion
Lion

Reputation: 872

How to sort NSMutableArray in ascending order, iPhone?

I have SQLite Database(DB) and made this to model class and taken the data of DB in NSMutableArray. My DB is similar to this DB. StudentName | RegisterNumber | DatesOfJoin(DoJ) I am displaying the DoJ in tableView successfully, but I wanna sort the DatesOfJoin in Ascending order and Descending order in tableView. Any help would be appreciated and Thanks you in advance.

Upvotes: 3

Views: 16329

Answers (5)

User558
User558

Reputation: 1175

    NSString *res =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    //Sorting an array
    NSMutableArray *sortArray = [res valueForKey:@"Year"];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
    yearArray = [sortArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

In Swift 3.0 Version

let sortedCapitalArray = yourArray.sorted {($0 as AnyObject).localizedCaseInsensitiveCompare(($1 as AnyObject)as! String) == ComparisonResult.orderedAscending}

Upvotes: 0

Ketan Ubhada
Ketan Ubhada

Reputation: 275

For Desending Order:

NSArray *sorted = [Arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    if ([obj1 intValue] < [obj2 intValue]) return NSOrderedDescending;
    else return NSOrderedAscending;
}];

For Asending Order:

NSArray *sorted = [Arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    if ([obj1 intValue] < [obj2 intValue]) return NSOrderedAscending;
    else return NSOrderedDescending;
}];

Upvotes: 3

Ajay Sharma
Ajay Sharma

Reputation: 4517

You can try using :

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"Over here your key" ascending:YES]; //Just write the key for which you want to have sorting & whole array would be sorted.
[self.messageAry sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
[descriptor release];

Hope that works,

Upvotes: 1

unexpectedvalue
unexpectedvalue

Reputation: 6139

You can use NSSortDescriptor and sortUsingDescriptors:sortDescriptors to sort a mutable array.

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"DatesOfJoin" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[array sortUsingDescriptors:sortDescriptors];
[sortDescriptor release];

Upvotes: 18

Macmade
Macmade

Reputation: 54059

Take a look at the official documentation: http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSArray_Class/NSArray.html

You got a lot of methods to sort the array:

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context

- (NSArray *)sortedArrayUsingSelector:(SEL)comparator

- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr

- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr

Upvotes: 4

Related Questions