How to sort an array in descending order in iPhone

This question already has an answer here:

  • How do I sort an NSMutableArray with custom objects in it? 25 answers
  • Sorting an NSArray in reverse order 2 answers

  • From the docs:

    An instance of NSSortDescriptor describes a basis for ordering objects by specifying the property to use to compare the objects, the method to use to compare the properties, and whether the comparison should be ascending or descending . Instances of NSSortDescriptor are immutable.

    You construct an instance of NSSortDescriptor by specifying the key path of the property to be compared, the order of the sort (ascending or descending) , and (optionally) a selector to use to perform the comparison.

    The constructor: initWithKey:ascending:

    Returns an NSSortDescriptor object initialized with a given property key path and sort order, and with the default comparison selector. - (id)initWithKey:(NSString *)keyPath ascending:(BOOL)ascending

    The parameter that specifies the order:

    ascending YES if the receiver specifies sorting in ascending order, otherwise NO.

    And these apple docs guide you step by step on how to sort arrays:

    https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/SortDescriptors/Articles/Creating.html


    使用以下内容:

    [yourArrayToBeSorted sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
        return [(NSDate *)obj2 compare:(NSDate *)obj1];} ];
    
    链接地址: http://www.djcxy.com/p/70822.html

    上一篇: 目标C中的排序

    下一篇: 如何在iPhone中以降序排列数组