Swift sort array of objects based on boolean value

前端 未结 3 959
陌清茗
陌清茗 2020-12-09 07:30

I\'m looking for a way to sort a Swift array based on a Boolean value.

I\'ve got it working using a cast to NSArray:

var boolSort = NSSortDescriptor(         


        
3条回答
  •  执念已碎
    2020-12-09 07:49

    Swift’s standard library provides a function called sorted, which sorts an array of values of a known type, based on the output of a sorting closure that you provide

    reversed = sorted(array) { $0 > $1 }
    

    reversed will be a new array which will be sorted according to the condition given in the closure.

提交回复
热议问题