由于我是swift的新手,我想对 cityName 键进行排序。
var arrCityList: [[String: Any]] = []
CityList:
[["pk_cityID": 1, "cityName": Ahmedabad], ["pk_cityID": 25, "cityName": Dahod], ["pk_cityID": 39, "cityName": Antah], ["pk_cityID": 44, "cityName": Bahror], ["pk_cityID": 117, "cityName": Jahazpur]]
arrCityList
包含 CityList 类型的数据。
我做了很多 SOF ,但我没有找到答案。
有人请帮助我找到解决方案,因为我被困在这上面。
先谢谢。
答案 0 :(得分:4)
你可以尝试
var arrCityList: [[String: Any]] = [["pk_cityID": 1, "cityName": "Ahmedabad"], ["pk_cityID": 25, "cityName": "Dahod"], ["pk_cityID": 39, "cityName": "Antah"], ["pk_cityID": 44, "cityName": "Bahror"], ["pk_cityID": 117, "cityName": "Jahazpur"]]
arrCityList.sort { ($0["cityName"] as! String) < ($1["cityName"] as! String) }
print(arrCityList)
答案 1 :(得分:1)
var arrCityList = [[String: Any]]()
arrCityList.append(["pk_cityID": 1, "cityName": "Ahmedabad"])
arrCityList.append(["pk_cityID": 25, "cityName": "Dahod"])
arrCityList.append(["pk_cityID": 39, "cityName": "Antah"])
arrCityList.append(["pk_cityID": 44, "cityName": "Bahror"])
arrCityList.append(["pk_cityID": 117, "cityName": "Jahazpur"])
arrCityList.sort {
let cityNameA = $0.0["cityName"] as! String
let cityNameB = $0.1["cityName"] as! String
return cityNameA < cityNameB
}
print(arrCityList)
// [
// ["pk_cityID": 1, "cityName": "Ahmedabad"],
// ["pk_cityID": 39, "cityName": "Antah"],
// ["pk_cityID": 44, "cityName": "Bahror"],
// ["pk_cityID": 25, "cityName": "Dahod"],
// ["pk_cityID": 117, "cityName": "Jahazpur"],
// ["pk_cityID": 117, "cityName": "Jahazpur"]
// ]