这个问题已经有了答案:
Sort array by order of values in other array [duplicate]
4个答案
所以我有两个数组

let knownOrder = ["Headache level", "Headache side", "Nausea", "Aura", "Phonophobia" ,"Photophobia"]

let tmpArray = ["Aura","Headache side","Photophobia"]

注意-tmparray可以包含所有或部分“knownorder”数组对象。
现在我要做的是根据“knownorder”对“tmparray”进行排序。
我能做什么?

最佳答案

过滤knownOrder以移除任何不在tmpArray中的元素:

let knownOrder = ["Headache level", "Headache side", "Nausea", "Aura", "Phonophobia" ,"Photophobia"]

let tmpArray = ["Aura","Headache side","Photophobia"]

let ordered = knownOrder.filter { tmpArray.contains($0) }
print(ordered)

[“头痛面”,“光环”,“畏光”]

08-06 07:29