Sort an NSArray in Descending Order

前端 未结 11 1196
死守一世寂寞
死守一世寂寞 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条回答
  •  Happy的楠姐
    2020-12-08 02:56

    There are a few ways:

    1. Write a comparison function and pass it to sortUsingFunction:context:.
    2. Use sortUsingComparator:, which takes a block. (Only available in Mac OS X 10.6 and later and iOS 4.0 and later.)
    3. Use sortUsingDescriptors:, and pass an array of at least one sort descriptor whose ascending property is false.

    In the first two cases, your comparison should simply return the negation of what the comparator you normally use (e.g., compare:) returned. The last one involves less custom code, so it's what I'd use.

提交回复
热议问题