我想根据它的重量对这些词进行排序。怎么做?
//这是字典
let words: [String:AnyObject] = [
"a" : [["name":"apple", "weight": "2"],["name" : "ant", "weight": "1"],["name": "animal", "weight": "3"]],
"b" : [["name":"bat", "weight": "4"],["name" : "ball", "weight": "2"],["name": "blue", "weight": "1"]],
"c" : [["name":"cat", "weight": "6"],["name" : "cow", "weight": "5"],["name": "crown", "weight": "8"], ["name": "camel", "weight": "7"]],
"d" : [["name":"dog", "weight": "3"],["name" : "donkey", "weight": "2"]],
"e" : [["name":"elephant", "weight": "2"],["name":"egg", "weight": "4"],["name":"e", "weight": "3"]]
]
由于
答案 0 :(得分:2)
您无法对字典本身进行排序,但您可以分别对字典中的数组进行排序。
为方便起见,使用了结构Item
struct Item {
let name : String
let weight : Int
}
var words: [String:[Item]] = [
"a" : [Item(name:"apple", weight:2), Item(name:"ant", weight:1), Item(name:"animal", weight:3)],
"b" : [Item(name:"bat", weight:4), Item(name:"ball", weight:2), Item(name:"blue", weight:1)],
"c" : [Item(name:"cat", weight:6), Item(name:"cow", weight:5), Item(name:"crown", weight:8), Item(name:"camel", weight:7)],
"d" : [Item(name:"dog", weight:3), Item(name:"donkey", weight:2)],
"e" : [Item(name:"elephant", weight:2),Item(name:"egg", weight:4),Item(name:"e", weight:3)]
]
for (key, value) in words {
let sortedArray = value.sort{$0.weight < $1.weight}
words[key] = sortedArray
}
print(words)
答案 1 :(得分:2)
鉴于此输入
let data: [String : [[String: String]]] = [
"a" : [["name":"apple", "weight": "2"],["name" : "ant", "weight": "1"],["name": "animal", "weight": "3"]],
"b" : [["name":"bat", "weight": "4"],["name" : "ball", "weight": "2"],["name": "blue", "weight": "1"]],
"c" : [["name":"cat", "weight": "6"],["name" : "cow", "weight": "5"],["name": "crown", "weight": "8"], ["name": "camel", "weight": "7"]],
"d" : [["name":"dog", "weight": "3"],["name" : "donkey", "weight": "2"]],
"e" : [["name":"elephant", "weight": "2"],["name":"egg", "weight": "4"],["name":"e", "weight": "3"]]
]
喜欢这个
struct Word {
let name: String
let weight: Int
init?(dict: [String:String]) {
guard let
name = dict["name"],
weightString = dict["weight"],
weight = Int(weightString)
else { return nil }
self.name = name
self.weight = weight
}
}
现在只需要减少/映射/排序
let dictOfSortedWords = data.reduce([String:[Word]]()) { (res, elm) -> [String:[Word]] in
var res = res
res[elm.0] = elm.1.flatMap(Word.init).sort { $0.weight > $1.weight }
return res
}
print(dictOfSortedWords["a"])
// [Word(name: "animal", weight: 3), Word(name: "apple", weight: 2), Word(name: "ant", weight: 1)]
print(dictOfSortedWords["b"])
// [Word(name: "bat", weight: 4), Word(name: "ball", weight: 2), Word(name: "blue", weight: 1)]
print(dictOfSortedWords["c"])
// [Word(name: "crown", weight: 8), Word(name: "camel", weight: 7), Word(name: "cat", weight: 6), Word(name: "cow", weight: 5)]
print(dictOfSortedWords["d"])
// [Word(name: "dog", weight: 3), Word(name: "donkey", weight: 2)]
print(dictOfSortedWords["e"])
//[Word(name: "egg", weight: 4), Word(name: "e", weight: 3), Word(name: "elephant", weight: 2)]
答案 2 :(得分:1)
这种数据结构的设计非常糟糕。这使得它不必要地难以使用。它滥用字典,强制解包,字符串解析为int ...它只是坏事。不要这样做。
请参阅我的其他答案,其中提出了一种明显更好的方法(它会在几分钟内完成)。
let dict = [ //has type [String : [[String : String]]]... madness!
"a" : [
["name":"apple", "weight": "2"],
["name" : "ant", "weight": "1"],
["name": "animal", "weight": "3"]
],
"b" : [
["name":"bat", "weight": "4"],
["name" : "ball", "weight": "2"],
["name": "blue", "weight": "1"]
],
"c" : [
["name":"cat", "weight": "6"],
["name" : "cow", "weight": "5"],
["name": "crown", "weight": "8"],
["name": "camel", "weight": "7"]
],
"d" : [
["name":"dog", "weight": "3"],
["name" : "donkey", "weight": "2"]
],
"e" : [
["name":"elephant", "weight": "2"],
["name":"egg", "weight": "4"],
["name":"e", "weight": "3"]
]
]
var sortedDict = dict
for (firstLetter, items) in sortedDict {
let sortedArray = items.sorted{ a, b in //"sorted" in Swift 3
let weightA = Int(a["weight"]!)!
let weightB = Int(b["weight"]!)!
return weightA < weightB
}
sortedDict[firstLetter] = sortedArray
}
print(sortedDict)
答案 3 :(得分:1)
这是一个更好的解决方案,可以很好地将数据存储在Item
结构中。处理这种方式要容易得多
struct Item {
let name: String
let weight: Int
}
let items = [ //has type [String : [Item]]
"a" : [
Item(name: "apple", weight: 2),
Item(name: "ant", weight: 1),
Item(name: "animal", weight: 3),
],
"b" : [
Item(name: "bat", weight: 4),
Item(name: "ball", weight: 2),
Item(name: "blue", weight: 1),
],
"c" : [
Item(name: "cat", weight: 6),
Item(name: "cow", weight: 5),
Item(name: "crown", weight: 8),
Item(name: "camel", weight: 7),
],
"d" : [
Item(name: "dog", weight: 3),
Item(name: "donkey", weight: 2),
],
"e" : [
Item(name: "elephant", weight: 2),
Item(name: "egg", weight: 4),
Item(name: "e", weight: 3),
]
]
for (firstLetter, itemArray) in items {
items[firstLetter] = itemArray.sort{$0.weight < $1.weight} //"sorted" in Swift 3
}
print(sortedItems)