Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Swift:数组排序不正确_Arrays_Sorting_Swift - Fatal编程技术网

Arrays Swift:数组排序不正确

Arrays Swift:数组排序不正确,arrays,sorting,swift,Arrays,Sorting,Swift,我使用它在Swift中对数组中的组件进行排序: myArray = myArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending } 但是,它给了我以下结果: [18C, 18L, 18R, 22, 24, 27, 36C, 36L, 36R, 4, 6, 9] 是否有可能以正确的方式进行分类,即 [4, 6, 9, 18C, 18L, 18R, 22, 24, 2

我使用它在Swift中对数组中的组件进行排序:

myArray = myArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
但是,它给了我以下结果:

[18C, 18L, 18R, 22, 24, 27, 36C, 36L, 36R, 4, 6, 9]
是否有可能以正确的方式进行分类,即

[4, 6, 9, 18C, 18L, 18R, 22, 24, 27, 36C, 36L, 36R]

您可以使用
比较
。数值搜索

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


在Swift 2中,
sorted()
已替换为
sort()
。如果条目以字符串而不是数字开头,则此操作无效。@MountainMan:这不是真的。使用数组
[“file1.txt”、“file10.txt”、“file2.txt”]
尝试一下。您能为使用它编写扩展名吗?是的,如果您愿意,可以。
let array2 = array.sort { $0.compare($1, options: .NumericSearch) == .OrderedAscending }