Swift: sorting of array is not done correctly

前端 未结 1 725
梦如初夏
梦如初夏 2021-01-20 02:02

I am using this to sort components in an array in Swift:

myArray = myArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscen         


        
相关标签:
1条回答
  • 2021-01-20 02:54

    You can use compare with .NumericSearch:

    array.sortInPlace { $0.compare($1, options: .NumericSearch) == .OrderedAscending }
    

    or

    let array2 = array.sort { $0.compare($1, options: .NumericSearch) == .OrderedAscending }
    
    0 讨论(0)
提交回复
热议问题