Swift 2.3
Alright folks. Assuming i have a Class named Post
, and array of Posts, [Post]
.
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 Post
objects inside my array), to sort our [Post]
array, by our scheduledTime
(Int) property?
Was always curious on how to approach this kind of questions. Thanks!
Just use sorted(by:)
with a closure that compares the first argument's scheduledTime
to the second's.
let sortedPosts = myPosts.sorted { $0.scheduledTime < $1.scheduledTime }
sorted(by:)
used to be called sort(_:)
, but the idea is the same:
let sortedPosts = myPosts.sort { $0.scheduledTime < $1.scheduledTime }