我正在尝试对由字典组成的Swift数组进行排序。我在下面准备了一个工作示例。目标是通过字典中的“d”元素对整个数组进行排序。我准备了这个可以放入Swift项目的工作示例:
var myArray = Array<AnyObject>()
var dict = Dictionary<String, AnyObject>()
dict["a"] = "hickory"
dict["b"] = "dickory"
dict["c"] = "dock"
dict["d"] = 5
myArray.append(dict)
dict["a"] = "three"
dict["b"] = "blind"
dict["c"] = "mice"
dict["d"] = 6
myArray.append(dict)
dict["a"] = "larry"
dict["b"] = "moe"
dict["c"] = "curly"
dict["d"] = 2
myArray.append(dict)
println(myArray[0])
println(myArray[1])
println(myArray[2])
}
这导致以下输出到日志:
{
a = hickory;
b = dickory;
c = dock;
d = 5;
}
{
a = three;
b = blind;
c = mice;
d = 6;
}
{
a = larry;
b = moe;
c = curly;
d = 2;
}
目标是通过“d”元素对数组进行排序,以便将上面的输出更改为以下内容(基于数字顺序“d”:'2,5,6'):< / p>
{
a = larry;
b = moe;
c = curly;
d = 2;
}
{
a = hickory;
b = dickory;
c = dock;
d = 5;
}
{
a = three;
b = blind;
c = mice;
d = 6;
}
还有其他一些看似相似的问题,但是当你看到它们时,很明显他们没有解决这个问题。谢谢你的帮助。
答案 0 :(得分:18)
要声明,如果需要将其保留为AnyObject,则必须显式转换:
var myArray = Array<AnyObject>()
var dict = Dictionary<String, AnyObject>()
dict["a"] = ("hickory" as! AnyObject)
dict["b"] = ("dickory" as! AnyObject)
dict["c"] = ("dock" as! AnyObject)
dict["d"] = (6 as! AnyObject)
myArray.append(dict as! AnyObject)
dict["a"] = ("three" as! AnyObject)
dict["b"] = ("blind" as! AnyObject)
dict["c"] = ("mice" as! AnyObject)
dict["d"] = (5 as! AnyObject)
myArray.append(dict as! AnyObject)
dict["a"] = ("larry" as! AnyObject)
dict["b"] = ("moe" as! AnyObject)
dict["c"] = ("curly" as! AnyObject)
dict["d"] = (4 as! AnyObject)
myArray.append(dict as! AnyObject)
如果没有追加,你可以这样做:
var myArray: [AnyObject] = [ ([
"a" : ("hickory" as! AnyObject),
"b" : ("dickory" as! AnyObject),
"c" : ("dock" as! AnyObject),
"d" : (6 as! AnyObject)
] as! AnyObject), ([
"a" : ("three" as! AnyObject),
"b" : ("blind" as! AnyObject),
"c" : ("mice" as! AnyObject),
"d" : (5 as! AnyObject)
] as! AnyObject), ([
"a" : ("larry" as! AnyObject),
"b" : ("moe" as! AnyObject),
"c" : ("curly" as! AnyObject),
"d" : (4 as! AnyObject)
] as! AnyObject)
]
这会给你相同的结果。虽然,如果只需要更改字典中的值对象,则不需要转换数组的元素:
var myArray: [Dictionary<String, AnyObject>] = [[
"a" : ("hickory" as! AnyObject),
"b" : ("dickory" as! AnyObject),
"c" : ("dock" as! AnyObject),
"d" : (6 as! AnyObject)
], [
"a" : ("three" as! AnyObject),
"b" : ("blind" as! AnyObject),
"c" : ("mice" as! AnyObject),
"d" : (5 as! AnyObject)
], [
"a" : ("larry" as! AnyObject),
"b" : ("moe" as! AnyObject),
"c" : ("curly" as! AnyObject),
"d" : (4 as! AnyObject)
]
]
然后,要进行排序,可以使用sort()闭包,它会对数组进行排序。你提供的闭包有两个参数(名为$ 0和$ 1),并返回一个Bool。如果在$ 1之前订购$ 0,则闭包应返回true,否则返回false。要做到这一点,你必须付出太多的代价:
//myArray starts as: [
// ["d": 6, "b": "dickory", "c": "dock", "a": "hickory"],
// ["d": 5, "b": "blind", "c": "mice", "a": "three"],
// ["d": 4, "b": "moe", "c": "curly", "a": "larry"]
//]
myArray.sort{
(($0 as! Dictionary<String, AnyObject>)["d"] as? Int) < (($1 as! Dictionary<String, AnyObject>)["d"] as? Int)
}
//myArray is now: [
// ["d": 4, "b": "moe", "c": "curly", "a": "larry"],
// ["d": 5, "b": "blind", "c": "mice", "a": "three"],
// ["d": 6, "b": "dickory", "c": "dock", "a": "hickory"]
//]
答案 1 :(得分:7)
在Swift 3和4中对字典进行排序
import
答案 2 :(得分:5)
var myArray: [AnyObject] = []
var dict:[String: AnyObject] = [:]
dict["a"] = "hickory"
dict["b"] = "dickory"
dict["c"] = "dock"
dict["d"] = 5
myArray.append(dict)
dict["a"] = "three"
dict["b"] = "blind"
dict["c"] = "mice"
dict["d"] = 6
myArray.append(dict)
dict["a"] = "larry"
dict["b"] = "moe"
dict["c"] = "curly"
dict["d"] = 2
myArray.append(dict)
let myArraySorted = myArray.sorted{$1["d"] as? Int > $0["d"] as? Int} as! [[String: AnyObject]]
println(myArraySorted) // [[b: moe, a: larry, d: 2, c: curly], [b: dickory, a: hickory, d: 5, c: dock], [b: blind, a: three, d: 6, c: mice]]"
或
var myArray: [[String:AnyObject]] = []
var dict:[String: AnyObject] = [:]
dict["a"] = "hickory"
dict["b"] = "dickory"
dict["c"] = "dock"
dict["d"] = 5
myArray.append(dict)
dict["a"] = "three"
dict["b"] = "blind"
dict["c"] = "mice"
dict["d"] = 6
myArray.append(dict)
dict["a"] = "larry"
dict["b"] = "moe"
dict["c"] = "curly"
dict["d"] = 2
myArray.append(dict)
let myArraySorted = myArray.sorted{$1["d"] as? Int > $0["d"] as? Int}
println(myArraySorted) // "[[b: moe, a: larry, d: 2, c: curly], [b: dickory, a: hickory, d: 5, c: dock], [b: blind, a: three, d: 6, c: mice]]"
答案 3 :(得分:1)
当我们解析数据时,我们可以使用NSSortDescriptor
进行排序let dataDict1 = responseDict.valueForKey("Data")
self.customerArray = dataDict1!.valueForKey("Customers") as! NSMutableArray
var tempArray = NSMutableArray()
for index in self.customerArray {
tempArray.addObject(index.valueForKey("Customer") as! NSMutableDictionary)
}
let descriptor: NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true, selector: "caseInsensitiveCompare:")
let sortedResults: NSArray = tempArray.sortedArrayUsingDescriptors([descriptor])
self.customerArray = NSMutableArray(array: sortedResults)
答案 4 :(得分:0)
在Swift中
let mySortedArray = myArray.sorted(by: {(int1, int2) -> Bool in
return ((int1 as! NSDictionary).value(forKey: "d") as! Int) < ((int2 as! NSDictionary).value(forKey: "d") as! Int) // It sorted the values and return to the mySortedArray
})
print(mySortedArray)
myArray.removeAllObjects() // Remove all objects and reuse it
myArray.addObject(from: mySortedArray)
print(mySortedArray)
按升序排列字典值数组很容易。它不需要任何循环。