naarray查询 swift_Swift用字符串和数字对数组进行排序

I have an array of strings,

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

I would like to get the output sorted in ascending as,

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

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? }

What would be the simple way to get this?

解决方案

edit/update: Xcode 10.2.x • Swift 5

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"]

or using the method sort(by:) on a MutableCollection:

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"]

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"]

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"]

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"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值