基于另一个数组的Swift Sort数组

时间:2018-02-14 08:14:36

标签: arrays swift sorting

所以我有2个阵列

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

let tmpArray = ["Aura","Headache side","Photophobia"]
  • 注意 - tmpArray可以包含全部或部分已知订单'数组对象。

现在我要做的是对“tmpArray”进行排序。按照“已知订单”的顺序排列。

我该怎么办?

1 个答案:

答案 0 :(得分:4)

过滤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)
  

["头痛方面","灵气","畏光"]