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
1 Answer 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
lang-swift
set
values?