chinese number sort(中文数字排序)

let numbers = [
    "一",
    "十三",
    "二",
    "四",
    "六",
    "十一"
]

extension String {
    fileprivate var toInt: Int {
        let numberFormatter = NumberFormatter()
        numberFormatter.locale = Locale(identifier: "zh_Hans_CN")
        numberFormatter.numberStyle = .spellOut
        let number = numberFormatter.number(from: self)
        return number?.intValue ?? 0
    }
}

let result = numbers.sorted { (a, b) -> Bool in
    a.toInt < b.toInt
}
/// result ["一", "二", "四", "六", "十一", "十三"]

你可能感兴趣的:(chinese number sort(中文数字排序))