Sort an NSArray in Descending Order

前端 未结 11 1226
死守一世寂寞
死守一世寂寞 2020-12-08 02:23

I have an NSArray of NSNumber objects that I have successfully sorted in ascending order using the following:

[myArray sortedArrayU         


        
11条回答
  •  情话喂你
    2020-12-08 03:12

    Another way, imo, is also nice: write another reverseCompare with category:

    @implementation NSNumber (Utility)
    
    - (NSComparisonResult)reverseCompare:(NSNumber *)aNumber {
      return [aNumber compare:self];
    }
    

    The good thing is that you can reuse everywhere and don't have to write the loop with reverse iterate. The bad thing is that you have to write more code:)

提交回复
热议问题