How to sort an array in Swift

后端 未结 8 1940
陌清茗
陌清茗 2020-12-13 04:06

I want the Swift version of this code:

NSArray *sortedNames = [names sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
8条回答
  •  情话喂你
    2020-12-13 04:55

    If you want to sort your array in ascending order then use below syntax:

    var arrayName = sorted(arrayName, <)
    

    as the sorted() is the predefined function in swift and < is used to indicate that the array should be sorted in ascending order. If you want to sort the array in descending order then simply replace < with > as I have shown below:

    var arrayName = sorted(arrayName, >)
    

提交回复
热议问题