Swift 2: Sort Object/Multi-Dimensional Array

Farhad

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.

MirekE

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SWIFT 2 - Sort Multi dimensional array by date

From Dev

Swift Sort Two Dimensional Array

From Dev

How i can sort 2 dimensional array?

From Dev

compare 2 dimensional string array with std::sort

From Dev

Python: Sort 2-Dimensional Array by Array length?

From Dev

Python: Sort 2-Dimensional Array by Array length?

From Dev

Using usort() to sort a 2-dimensional array with hierarchical ordering criteria

From Dev

Radix sort Java using 2 dimensional array recursive

From Dev

Bubble Sort Two Dimensional Array

From Dev

sort multi dimensional array by date

From Dev

Write a program that uses Bubble Sort to sort integers in a 2 dimensional array in ascending order

From Dev

convert 3 dimensional array to a 2 dimensional array

From Dev

Filter 2 dimensional array

From Dev

2 dimensional array PHP

From Dev

atoi() for 2 dimensional array

From Dev

2 dimensional array of TCHAR

From Java

Two-dimensional array in Swift

From Dev

Init a 3 dimensional array with 2 dimensional arrays

From Dev

Naturally sort a multi-dimensional array by key

From Dev

How to sort two-dimensional array in python

From Java

How to Sort Multi-dimensional Array by Value?

From Dev

Sort a multi-dimensional Array in PHP

From Dev

PHP array sort mutli-dimensional

From Dev

How to sort two dimensional array lexicographically?

From Dev

PHP: Sort multi-dimensional array by date

From Dev

Multi-Dimensional array sort first and second

From Dev

Sort Multi dimensional array using values

From Dev

I want to sort 3 dimensional array in php

From Dev

Sort a multi dimensional array with dates as keys in PHP