ios 快速按字母顺序排列数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36394813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting of an array alphabetically in swift
提问by anusha hrithi
I am new to swift.I am trying one sample app in which I need to implement the sorting of an array in alphabetical order.I getting the json data and I am adding the titles in the array.Now i would like to sort that alphabetically.Here is my code .....
我是 swift 的新手。我正在尝试一个示例应用程序,我需要在其中按字母顺序对数组进行排序。我获取 json 数据并在数组中添加标题。现在我想按字母顺序排序.这是我的代码.....
func updateSearchResults(data: NSData?)
{
do
{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let blogs: NSArray = json["results"] as? [AnyObject] {
print(blogs)
for blog in blogs {
if let name = blog["original_title"] as? String {
names.addObject(name)
}
}
print(names)
**let sortedArray = sorted(names, {
(str1: String, str2: String) -> Bool in
return str1.toInt() < str2.toInt()** // Here I am getting the Error Message
})
}
}
catch {
print("error serializing JSON: \(error)")
}
}
The error message I am getting is "Cannot invoke 'sorted' with an argument list of type '(NSMutableArray, (String, String) -> Bool)'"
我收到的错误消息是“无法使用类型为 '(NSMutableArray, (String, String) -> Bool)' 的参数列表调用 'sorted'”
I tried a lot to achieve this but I didn't find the solution. Can anyone help me to resolve this issue. Thanks In Advance.
我尝试了很多来实现这一目标,但我没有找到解决方案。谁能帮我解决这个问题。提前致谢。
回答by Payal Maniyar
First convert NSMutableArray
to the Array by using below line of code.
首先NSMutableArray
使用下面的代码行转换为数组。
let swiftArray = mutableArray as AnyObject as! [String]
Use below line of code to sort the Array.
使用下面的代码行对数组进行排序。
var sortedArray = names.sorted { var sortedArray = swiftArray.sorted { let sortedNames = names.sort { let sortedNames = names.sorted(by: <)
.name < .name }
.localizedCaseInsensitiveCompare() == ComparisonResult.orderedAscending }
.localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }
Check below link for sort
Closures.
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
检查以下链接以了解sort
关闭。
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
Update for Swift 3.0
Swift 3.0 更新
var names = [ "Alpha", "alpha", "bravo", "beta"]
var sortedNames = names.sorted { items = items.sorted(by: { (item1, item2) -> Bool in
return item1.product.name.compare(item2.product.name) == ComparisonResult.orderedAscending
})
.localizedCaseInsensitiveCompare() == ComparisonResult.orderedAscending }
print(sortedNames) //Logs ["Alpha", "alpha","beta", "bravo"]
回答by Moin Shirazi
Use this simple code of line to sort ur array
使用这个简单的行代码对你的数组进行排序
"DATA": [
{
email = "[email protected]";
firstname = Harvey
},
{
email = "[email protected]";
firstname = poonam
},
{
email = "[email protected]";
firstname = rahul
},
{
email = "[email protected]";
firstname = Chulbulx
},
{
email = "[email protected]";
firstname = rahul
},
{
email = "[email protected]";
firstname = Jay
},
{
email = "[email protected]";
firstname = Roy
},
{
email = "[email protected]";
firstname = Regan
},
{
email = "[email protected]";
firstname = Jaydip
}
]
For Swift 4you can use only this
对于Swift 4,你只能使用这个
self.aryNameList = self.aryNameList.sorted(by: { (Obj1, Obj2) -> Bool in
let Obj1_Name = Obj1.firstname ?? ""
let Obj2_Name = Obj2.firstname ?? ""
return (Obj1_Name.localizedCaseInsensitiveCompare(Obj2_Name) == .orderedAscending)
})
回答by kolisko
Swift4
斯威夫特4
let yourStringArray = [ "beTA", "ALPha", "Beta", "Alpha"]
var sortedArray = yourStringArray.sorted()
// Result will be ["ALPha", "Alpha", "Beta", "beTA"]
回答by Giggs
For an array of objects:
对于对象数组:
var names = [ "Alpha", "alpha", "bravo"]
var sortedNames = names.sort { ##代码##.localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }
print(sortedNames) //Logs ["Alpha", "alpha", "bravo"]
回答by Krunal Patel
Swift 4(working code)
Swift 4(工作代码)
JSON response -> Stored in aryNameList
JSON 响应 -> 存储在 aryNameList 中
##代码##Code
代码
##代码##working every case (for ex: lowerCase, upperCase..)
处理每种情况(例如:lowerCase、upperCase ..)
回答by Erik Nguyen
Swift 3 solution:
斯威夫特 3 解决方案:
##代码##Creds to jjatie
对 jjatie 的信用
回答by Droid GEEK
Try this one
试试这个
##代码##