基于在另一个数组值的对象排序数组对应于特定属性 [英] Sort array of objects based on values in another array that correspond to specific properties
问题描述
假设检索对象从一个JSON API的数组:
[
{
ID:48,
名:鲍勃
},
{
ID:198,
名:戴夫
},
{
ID:2301,
名:艾米
},
{
ID:990,
名:科莱特
}
]//例如为了易于再现的:让数据对象= [
[ID:48,名:鲍勃],
[ID:198,名:戴夫],
[ID:2301,名:艾米],
[ID:990,名:科莱特]
]
在客户端,我想允许用户重新排列这些对象。要保存命令,我会存储在数组中ID的列表:
让指数:[INT] = [48,990,2103,198]
我怎样才能重新排序的IDS在选别指标的顺序数据对象的原始数组?
dataObjects.sort({
//奇迹发生在这里也许?
}
因此,在年底,我得到:
打印(数据对象)
/ *
[
[ID:48,名:鲍勃],
[ID:990,名:科莱特],
[ID:2301,名:艾米],
[ID:198,名:戴夫]
]
/ *
方法A)数据解析成一个简单的字典,其中对字典的关键是用来排序它的ID:
FUNC排序< VALUE>(A:[INT:值],支持算法FMP B:INT]) - GT; [(智力,值)] {
返回a.sort {X,Y在
b.indexOf(X.0)LT; b.indexOf(y.0)
}
}// ....
让= [48:鲍勃,198:戴夫,2301:艾米,990:科莱特]
令b = [48,198,2301,990]
排序(一,支持算法FMP:B)
办法B)随着一些自定义的数据对象
键入:(可能是最好的办法)
结构数据对象{
让ID:诠释
让名:字符串 的init(_ ID:智力,_参数name:String){
self.id = ID
self.name =名称
}
}FUNC排序(A:[数据对象],支持算法FMP B:INT]) - GT; [数据对象] {
返回a.sort {X,Y在
b.indexOf(x.id)LT; b.indexOf(y.id)
}
}
让= [数据对象(48,鲍勃),数据对象(198,戴夫),数据对象(2301,艾米),数据对象(990,科莱特)]
令b = [48,198,2301,990]排序(一,支持算法FMP:B)
/ * [
数据对象(编号:48,店名:鲍勃),
数据对象(ID:990,名称:科莱特),
数据对象(ID:198,名称:戴夫),
数据对象(编号:2301,名称:艾米)
] * /
办法C)随着原材料的JSON值,它可以在一个不太干净的方式完成的:
FUNC排序< VALUE>(A:[[字符串:值],支持算法FMP B:INT]) - GT; [[字符串值]] {
返回a.sort {X,Y在
让XID = X [ID]作为! INT
让云南发展培训学院= Y [ID]作为! INT
返回b.indexOf(XID)LT; b.indexOf(YID)
}
}让数据对象= [
[ID:48,名:鲍勃],
[ID:198,名:戴夫],
[ID:2301,名:艾米],
[ID:990,名:科莱特]
]令b = [48,198,2301,990]排序(数据对象,支持算法FMP:B)
Let's say retrieve an array of objects from a JSON API:
[
{
"id": 48,
"name": "Bob"
},
{
"id": 198,
"name": "Dave"
},
{
"id": 2301,
"name": "Amy"
},
{
"id": 990,
"name": "Colette"
}
]
// e.g. for ease of reproduction:
let dataObjects = [
["id": 48, "name": "Bob"],
["id": 198, "name": "Dave"],
["id": 2301, "name": "Amy"],
["id": 990, "name": "Colette"]
]
On the client, I'd like to allow the user to re-order these objects. To save the order, I'll store a list of ids in an array:
let index: [Int] = [48, 990, 2103, 198]
How can I reorder the original array of data objects based on the order of ids in the sorting index?
dataObjects.sort({
// magic happens here maybe?
}
So that in the end, I get:
print(dataObjects)
/*
[
["id": 48, "name": "Bob"],
["id": 990, "name": "Colette"],
["id": 2301, "name": "Amy"],
["id": 198, "name": "Dave"]
]
/*
Approach A) Parsing the data into a simple dictionary where the key for the dictionary is the id used to sort it:
func sort<Value>(a: [Int: Value], basedOn b: [Int]) -> [(Int, Value)] {
return a.sort { x, y in
b.indexOf(x.0) < b.indexOf(y.0)
}
}
// ....
let a = [48:"Bob", 198:"Dave", 2301:"Amy", 990:"Colette"]
let b = [48, 198, 2301, 990]
sort(a, basedOn: b)
Approach B) With some custom DataObject
type: (probably best way)
struct DataObject {
let id: Int
let name: String
init(_ id: Int, _ name: String) {
self.id = id
self.name = name
}
}
func sort(a: [DataObject], basedOn b: [Int]) -> [DataObject] {
return a.sort { x, y in
b.indexOf(x.id) < b.indexOf(y.id)
}
}
let a = [DataObject(48, "Bob"), DataObject(198, "Dave"), DataObject(2301, "Amy"), DataObject(990, "Colette")]
let b = [48, 198, 2301, 990]
sort(a, basedOn: b)
/* [
DataObject(id: 48, name: "Bob"),
DataObject(id: 990, name: "Colette"),
DataObject(id: 198, name: "Dave"),
DataObject(id: 2301, name: "Amy")
] */
Approach C) With the raw json values, it can be done in a less "clean" way:
func sort<Value>(a: [[String: Value]], basedOn b: [Int]) -> [[String: Value]] {
return a.sort { x, y in
let xId = x["id"] as! Int
let yId = y["id"] as! Int
return b.indexOf(xId) < b.indexOf(yId)
}
}
let dataObjects = [
["id": 48, "name": "Bob"],
["id": 198, "name": "Dave"],
["id": 2301, "name": "Amy"],
["id": 990, "name": "Colette"]
]
let b = [48, 198, 2301, 990]
sort(dataObjects, basedOn: b)
这篇关于基于在另一个数组值的对象排序数组对应于特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!