Tableview is not sorting once number is above 10

此生再无相见时 提交于 2019-12-13 11:14:36

问题


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 method it's sorting numbers (aka points) all the way up to 10. But once a number is above 10, the list is not getting sorted anymore. And I've no idea how to solve this issue.

Result under 10:

Result above 10:

I guess it's being placed where it is because of "10", if I would use like "60" it would be placed under the 2nd or 3rd spot. So, how can I solve this? I would be very thankful for it.


回答1:


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 }


来源:https://stackoverflow.com/questions/55180270/tableview-is-not-sorting-once-number-is-above-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!