Roi Mulia Roi Mulia - 6 months ago 22
Swift Question

Swift - How does one sort an array by internal integer property in the most efficient way

Swift 2.3

Alright folks. Assuming i have a Class named

, and array of Posts,

Class Posts
var message : String!
var scheduledTime : Int!
var myPosts = [Posts]()...(500 objects)

How can i, in the most efficient/performance wise way(let's assume i have 500
objects inside my array), to sort our
array, by our
(Int) property?

Was always curious on how to approach this kind of questions. Thanks!


Just use sort(_:) with a closure that compares the first argument's scheduledTime to the second's.

let sortedPosts = myPosts.sort{ $0.scheduledTime < $1.scheduledTime }