I have an array that is loaded from MYSQL database using PHP and converting to JSON, the data get loaded into the table, but now I want to sort this data, how can I do this? I would like to sort them by latest time =
in my object array.
JSON Result got with NSURLSession
:
(
{
id = 8;
time = "2015-07-24 17:12:00";
title = "World is full of good people!";
},
{
id = 10;
time = "2015-07-24 18:44:30";
title = "One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head.";
}
)
I read some questions/answers on stackoverflow.com and people are saying I should use sorted
on my array, but I think that method is gone in swift 2.
This is what I get once i try it arr.sorted
'NSArray' does not have a member named 'sorted'
I find an apple sorting document, but couldn't find any good answer to describe on how to use it.
Once you have the data in a Swift array and the date stored as NSDate (which is not what you show above), you can use sortInPlace, for example:
s.sortInPlace { (a, b) -> Bool in
return a.time.timeIntervalSinceReferenceDate < b.time.timeIntervalSinceReferenceDate
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments