ios - Tableview is not sorting once number is above 10
问题描述
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.
解决方案
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 }
推荐阅读
- asp.net-core - 何时使用 LocalRedirect 与 RedirectToPage
- tensorflow - 张量流的估计器如何加载模型的特定步骤而不是最新的?
- java - 如何使用修复 XXE 漏洞时出现的 JAXB 修复此 Unmarshal Exception?
- sql - SQL 上传用 (id, value) 对替换整个列
- php - 无法从 MySQL 获取数据
- jolt - Jolt Transform 演示不适合我
- r - 使用 CDO 在 R 中重新网格化多个 netcdf 文件
- javascript - 在jsp页面中另一个选择选项的功能中,在选择html中显示列表
- spring-boot - Spring boot如何使用Hikari自动配置但在运行时设置用户名/密码
- python - Pytest PropertyMock 作为 MagicMock 的 return_value 没有被调用