1

So I have 2 arrays

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

let tmpArray = ["Aura","Headache side","Photophobia"]
  • Note - The tmpArray can include all or some of the 'knownOrder' array objects.

And now what i want to do is to sort the 'tmpArray' in order based on the 'knownOrder'.

What can I do?

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
dudi hisine
  • 129
  • 11

1 Answers1

4

Filter knownOrder to remove any element that is not in 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)

["Headache side", "Aura", "Photophobia"]

luk2302
  • 55,258
  • 23
  • 97
  • 137