я новичок в swift, я собираюсь отсортировать файл json в связке, а не по коду, есть ли способ сортировки файла по коду или нет, я хочу отсортировать его по "citname": "لومار"
json-файл:
{
"data":[
{
"_id":1,
"citproidint":4,
"citname":"لومار"
},
{
"_id":2,
"citproidint":4,
"citname":"ايوان"
},
{
"_id":3,
"citproidint":12,
"citname":"آبعلی"
},
{
"_id":4,
"citproidint":25,
"citname":"نيشابور"
},
{
"_id":5,
"citproidint":27,
"citname":"سقز"
},
]
}
... // 827 id находится в этом json-файле, и для каждого блока в этом json-файле я анализирую вот так, и все в порядке, но я хочучтобы использовать сортировку в моем окне выбора, тогда мне нужно сохранить отсортированные модалы
guard let path = Bundle.main.path(forResource: "citybig", ofType: "json") else { return }
// city Text file:// ...
let url = URL(fileURLWithPath: path)
do {
// data explained in bytes
let data = try Data(contentsOf: url)
// we get the json file text
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
// put json in nsdictinary so we can access each index and person sooo ... id and more
for (key,value) in json as! NSDictionary {
if (key as! String) == "data" {
let value2 = value.s
for each in value as! [[String: Any]] {
let singleCityModal = cityModal()
for (key,value) in each {
switch key {
case "_id" :
singleCityModal.id = value as? Int
case "citproidint" :
singleCityModal.citproidint = value as? Int
case "citname" :
singleCityModal.citname = value as? String
default:
break
}
}
cityFirstModal.append(singleCityModal)
}
cityFinalModal.append(contentsOf: cityFirstModal)