How to sort JSON in swift based on persian characters
i am new to swift , i going to sort a json file in bundle not by code , is there anyway to sort the file using by code or not , i want to sort it from "citname":"لومار"
the json file is :
{
"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 is in this json file
and for every block in this json file i parse like this , and everything is fine but i want to use it sorted in my pickerview then i need to save sorted modals
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)
NSDictionary
1 Answer
1
Your way to parse the data is quite strange, in Swift 4 there is a more convenient way, the Decodable
protocol.
Decodable
You need two structs:
struct CityData : Decodable {
private enum CodingKeys: String, CodingKey { case cities = "data"}
let cities : [City]
}
struct City : Decodable {
private enum CodingKeys: String, CodingKey { case id = "_id", citproidint, name = "citname"}
let id, citproidint: Int
let name : String
}
and the code to parse the JSON becomes a bit shorter (and more efficient)
let url = Bundle.main.url(forResource: "citybig", withExtension: "json")!
let data = try! Data(contentsOf: url)
let cityData = try! JSONDecoder().decode(CityData.self, from: data)
The exclamation marks are intended. As the (immutable) file is in the bundle the code must not crash. If it does you made a design mistake.
It's just one additional line to sort the cities by citproidint
citproidint
let sortedCities = cityData.cities.sorted(by: {$0.citproidint < $1.citproidint})
I'm not familiar with Farsi but if you want to sort by name
the selector localizedStandardCompare
might do the job
name
localizedStandardCompare
let sortedCities = cityData.cities.sorted(by: {$0.name.localizedStandardCompare($1.name) == .orderedAscending })
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You use Swift 4 according to your tag. So don't use
NSDictionary
. Avoid NSStuff when there is an equivalent in Swift, like Dictionary. Also, don't name your var with an uppercase. Now, what code did you try to sort?– Larme
6 hours ago