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
  • 510,973
  • 84
  • 1,183
  • 1,314
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
  • 50,400
  • 22
  • 92
  • 131