简体   繁体   English

如何按升序排序NSMutableArray,iPhone?

[英]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. 我有SQLite数据库(DB)并将其作为模型类并在NSMutableArray中获取数据库。 My DB is similar to this DB. 我的DB与此DB类似。 StudentName | 学生名称| RegisterNumber | 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. DatesOfJoin(DoJ)我在tableView中成功显示了DoJ,但我想在tableView中按升序和降序排序DatesOfJoin。 Any help would be appreciated and Thanks you in advance. 任何帮助将不胜感激,并提前感谢您。

You can use NSSortDescriptor and sortUsingDescriptors:sortDescriptors to sort a mutable array. 您可以使用NSSortDescriptorsortUsingDescriptors:sortDescriptors对可变数组进行排序。

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

Take a look at the official documentation: http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSArray_Class/NSArray.html 请查看官方文档: 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

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: 对于Asending Order:

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

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, 希望有效,

    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 Swift 3.0版本中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM