0

我有弦来

[
 "71ADDC23D2048",
 "9BF4F631EBA69",
 "B123454F4F631EBA69",
 "123454F4F6345FGT"
]

我当前的排序代码是:

// Sorting the Tans
func sortByTagID(arrayTags:[TansIDS],orderBy:String) -> [TansIDS] {

    if orderBy.uppercased() == OrderBy.Ascending {
        let sortedArray = arrayTags.sorted {String($0.trans-id!).localizedStandardCompare(String($1.trans-id!)) == .orderedAscending}
        return sortedArray
    }
    else {
        let sortedArray = arrayTags.sorted {String($0.trans-id!).localizedStandardCompare(String($1.trans-id!)) == .orderedDescending}
        return sortedArray
    }

}

请让我知道是否有比这更好的排序方法?因为当我与其他平台(Android)进行比较时,它并不相同。

4

1 回答 1

0

试试这个

func sortByTagID(arrayTags:[TansIDS],orderBy:String) -> [TansIDS] {

    if orderBy.uppercased() == OrderBy.Ascending {
        let sortedArray = arrayTags.sorted {String($0.trans-id!) < String($1.trans-id!)}
        return sortedArray
    }
    else {
        let sortedArray = arrayTags.sorted {String($0.trans-id!) > String($1.trans-id!)}
        return sortedArray
    }
}

输出升序:

[
 "123454F4F6345FGT",
 "71ADDC23D2048",
 "9BF4F631EBA69",
 "B123454F4F631EBA69"
] 

输出降序:

[
 "B123454F4F631EBA69",
 "9BF4F631EBA69",
 "71ADDC23D2048",
 "123454F4F6345FGT"
]

于 2019-11-21T05:30:52.627 回答