How to sort an array in Swift

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

I want the Swift version of this code:

NSArray *sortedNames = [names sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
8条回答
  •  渐次进展
    2020-12-13 05:00

    If your array does not contain Custom Objects (just a string or number type):

    var sortedNames = sorted(names, <)
    

    Otherwise if you create a Custom Data Object Class containing custom properties inside:

    customDataObjectArray.sort({ $0.customProperty < $1.customProperty })
    

提交回复
热议问题