NSMutable sort排序

來源:互聯網
上載者:User

標籤:des   http   io   os   ar   for   art   sp   on   

Compare method

Either you implement a compare-method for your object:

- (NSComparisonResult)compare:(Person *)otherObject {    return [self.birthDate compare:otherObject.birthDate];}NSArray *sortedArray;sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];
NSSortDescriptor (better)

or usually even better:

NSSortDescriptor *sortDescriptor;sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"                                              ascending:YES] autorelease];NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];NSArray *sortedArray;sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];

You can easily sort by multiple keys by adding more than one to the array. Using custom comparator-methods is possible as well. Have a look at the documentation.

Blocks (shiny!)

There‘s also the possibility of sorting with a block since Mac OS X 10.6 and iOS 4:

NSArray *sortedArray;sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {    NSDate *first = [(Person*)a birthDate];    NSDate *second = [(Person*)b birthDate];    return [first compare:second];}];

For this particular example I‘m assuming that the objects in your array have a ‘position‘ method, which returns an NSInteger.

NSArray *arrayToSort = where ever you get the array from... ;NSComparisonResult (^sortBlock)(id, id) = ^(id obj1, id obj2) {  if ([obj1 position] > [obj2 position]) {     return (NSComparisonResult)NSOrderedDescending;  }  if ([obj1 position] < [obj2 position]) {    return (NSComparisonResult)NSOrderedAscending;  }  return (NSComparisonResult)NSOrderedSame;};NSArray *sorted = [arrayToSort sortedArrayUsingComparator:sortBlock];

Note: the "sorted" array will be autoreleased.

If this ‘position‘ is in NSDictory.

NSComparisonResult (^sortBlock)(id, id) = ^(id obj1, id obj2) {

            NSInteger p1= [((NSString*) [obj1 objectForKey:@"position"]) integerValue];

            NSInteger p2= [((NSString*) [obj2 objectForKey:@"position"]) integerValue];

            if (p1 > p2) {

                return (NSComparisonResult)NSOrderedDescending;

            }

            if (p1 < p2) {

                return (NSComparisonResult)NSOrderedAscending;

            }

            return (NSComparisonResult)NSOrderedSame;

        };

NSMutable sort排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.