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?

0

1 Answer 1

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"]

Sign up to request clarification or add additional context in comments.

1 Comment

I have to do the same thing. But I have an array of objects. Any help on that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.