Swift sort array of objects based on boolean value

前端 未结 3 967
陌清茗
陌清茗 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:47

    New (for Swift 1.2)

    return results.sort { $0.selected && !$1.selected }
    

    Old (for Swift 1.0)

    Assuming results is of type [VDLProfile] and VDLProfile has a Bool member selected:

    return results.sorted { $0.selected < $1.selected }
    

    See documentation for sorted

提交回复
热议问题