gpt4 book ai didi

ios - 按键值对 Swift 字典数组进行排序

转载 作者:IT王子 更新时间:2023-10-29 05:22:35 26 4
gpt4 key购买 nike

我正在尝试对由字典组成的 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'):

{
a = larry;
b = moe;
c = curly;
d = 2;
}
{
a = hickory;
b = dickory;
c = dock;
d = 5;
}
{
a = three;
b = blind;
c = mice;
d = 6;
}

还有一些其他问题看起来很相似,但是当您查看它们时,很明显它们并没有解决这个问题。感谢您的协助。

最佳答案

要声明,如果你需要将它保持为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。如果 $0 在 $1 之前排序,则闭包应返回 true,如果在 $1 之后,则返回 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"]
//]

关于ios - 按键值对 Swift 字典数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30446812/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com