Tableview is not sorting once number is above 10

后端 未结 1 1151
长情又很酷
长情又很酷 2020-12-22 13:48

I\'m sorting tableview using this code:

list.sort() { $0.points > $1.points }

And everything works fine (For a bit) If I use this sort m

相关标签:
1条回答
  • 2020-12-22 14:28

    That's the normal behavior when comparing strings. Strings are not numbers.

    The method localizedStandardCompare compares like in Finder and handles numeric strings correctly

    list.sort() { $0.points.localizedStandardCompare($1.points) == .orderedDescending }
    

    Alternatively compare with .numeric option

    list.sort() { $0.points.compare($1.points, options: .numeric) == .orderedDescending }
    
    0 讨论(0)
提交回复
热议问题