Ios 使用当前位置值对对象数组进行排序
我有一个源对象数组:Ios 使用当前位置值对对象数组进行排序,ios,swift,sorting,Ios,Swift,Sorting,我有一个源对象数组: class Source: NSObject { var sourceName: String var country: String } 我想按国家名称对数组进行排序,这是我从NSLocale获得的 if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
class Source: NSObject {
var sourceName: String
var country: String
}
我想按国家名称对数组进行排序,这是我从NSLocale获得的
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}
如何首先使用国家名称(如当前位置对象)对对象进行排序,然后再使用其他名称
谢谢您可以做的是首先按照通常按国家/地区名称对数组进行排序:
array.sort{ $0.sourceName < $1.sourceName }
我可能没有使用您想要的正确变量,我不确定源变量是什么意思。
另外,确保“currentCountry”
字符串是您从方法中正确获得的。但它只对一个国家进行排序,但我有多个国家。
if let index = array.index(where: { $0.sourceName == "currentCountry" }) {
let source = array.remove(at: index)
array.insert(source, at: 0)
}