0

I have an array that's like,

var array = [[CustomModel(set:2, name: "Apple"), CustomModel(set:2, name:"Orange")], [CustomModel(set:1, name:"Home"),CustomModel(set:1, name:"Building")]]

how do I sort the array to be like

var array = [[CustomModel(set:1, name:"Home"),CustomModel(set:1, name:"Building")], [CustomModel(set:2, name: "Apple"), CustomModel(set:2, name:"Orange")]]

so that the value of the set that is lower comes before the value of others.

Is this even possible? or is there a better way to do this.

Jonathan Hall
80.3k19 gold badges161 silver badges206 bronze badges
asked Dec 11, 2019 at 21:38
2
  • What should happen if the inner arrays contain different set values? Commented Dec 11, 2019 at 21:49
  • It will always be the same since I am referencing it from the same database document Commented Dec 11, 2019 at 21:54

1 Answer 1

1

It's a plain sort if set is the same for all elements, then we can just pick the first element of the inner array to sort with

array.sort(by: {(0ドル.first?.set ?? Int.max) < (1ドル.first?.set ?? Int.max)})

otherwise we need to pick one, like the smallest value

array.sort(by: { (0ドル.map {0ドル.set}.min() ?? Int.max) < (1ドル.map {0ドル.set}.min() ?? Int.max) })
answered Dec 11, 2019 at 21:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.