Swift用字符串和数字对数组进行排序 [英] Swift sort an array with strings and numbers

查看:490
本文介绍了Swift用字符串和数字对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组,

let array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]

我想将输出升序排列,

let sorted = [ "1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA" ]

我尝试使用sorted命令,但是遇到超过2位数字(例如:100、101、200等)时,该命令不起作用

I tried using the sorted command but it does not work when it encounters more than 2 digits e.g.: 100, 101, 200 etc.

array.sorted { $0? < $1? }

简单的方法是什么?

推荐答案

编辑/更新: Xcode 10.2.x•Swift 5

您可以使用String方法localizedStandardCompare

You can use String method localizedStandardCompare

let array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
let sorted = array.sorted {$0.localizedStandardCompare($1) == .orderedAscending}

print(sorted) // ["1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA"]


或在MutableCollection上使用方法sort(by:):

var array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
array.sort {$0.localizedStandardCompare($1) == .orderedAscending}

print(array) // ["1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA"]


您还可以实现扩展Collection的自己的本地化标准排序方法:


You can also implement your own localized standard sort method extending Collection:

extension Collection where Element: StringProtocol {
    public func localizedStandardSorted(_ result: ComparisonResult) -> [Element] {
        return sorted { $0.localizedStandardCompare($1) == result }
    }
}


let array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
let sorted = array.localizedStandardSorted(.orderedAscending)

print(sorted) // ["1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA"]


变异方法以及对MutableCollection的扩展:


The mutating method as well extending MutableCollection:

extension MutableCollection where Element: StringProtocol, Self: RandomAccessCollection {
    public mutating func localizedStandardSort(_ result: ComparisonResult) {
        sort { $0.localizedStandardCompare($1) == result }
    }
}


var array = [ "10", "1", "101", "NA", "100", "20", "210", "200", "NA", "7" ]
array.localizedStandardSort(.orderedAscending)

print(array) // ["1", "7", "10", "20", "100", "101", "200", "210", "NA", "NA"]

如果需要对数组进行数字排序,则可以使用字符串比较方法,将options参数设置为.numeric:

If you need to sort your array numerically you can use String compare method setting the options parameter to .numeric:

public extension Collection where Element: StringProtocol {
    func sortedNumerically(_ result: ComparisonResult) -> [Element] {
        return sorted { $0.compare($1, options: .numeric) == result }
    }
}
public extension MutableCollection where Element: StringProtocol, Self: RandomAccessCollection {
    mutating func sortNumerically(_ result: ComparisonResult) {
        sort { $0.compare($1, options: .numeric) == result }
    }
}


var numbers = ["1.5","0.5","1"]
let sorted = numbers.sortedNumerically(.orderedAscending)
print(sorted)  // ["0.5", "1", "1.5"]
print(numbers) // ["1.5","0.5","1"]
// mutating the original collection
numbers.sortNumerically(.orderedDescending)
print(numbers)  // "["1.5", "1", "0.5"]\n"

这篇关于Swift用字符串和数字对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆