How to Sort an array in an ascending order swift 2.3

17,813

Solution 1

let array=[
        [
            "msg":"Hi This is Jecky",
            "name":"Susheel",
            "sender":77,
            "timestamp":1464241769520,
            "username":"susheel",
        ],
        [
            "msg":"Dubai",
            "name":"Jecky",
            "sender":78,
            "timestamp":1464246547147,
            "username":"Jecky",
        ],
        [
            "msg":"How are you ?",
            "name":"Susheel",
            "sender":77,
            "timestamp":1464243480381,
            "username":"susheel",
        ],
        [
            "msg":"Aje dekhai nai",
            "name":"Jecky",
            "sender":78,
            "timestamp":1464244974198,
            "username":"Jecky",
        ],
    ]
    print("array = \(array)")
    let sortedArray=array.sort { (obj1, obj2) -> Bool in
        return (obj1["timestamp"] as! Double) < (obj2["timestamp"] as! Double)
    }
    print("sortedArray = \(sortedArray)")

Solution 2

If your array is mutable you can user sortInPlace

yourArray.sortInPlace{$0.timestamp < $1.timestamp}

and if not, you can create a new array from sort, like suggested by Kristijan (although no need for parentheses on trailing closures):

let newArray = yourArray.sort{$0.timestamp < $1.timestamp}

Solution 3

You can get this functionality using extension:

extension NSArray{
 //sorting- ascending
  func ascendingArrayWithKeyValue(key:String) -> NSArray{
    let ns = NSSortDescriptor.init(key: key, ascending: true)
    let aa = NSArray(object: ns)
    let arrResult = self.sortedArray(using: aa as! [NSSortDescriptor])
    return arrResult as NSArray
  }

  //sorting - descending
  func discendingArrayWithKeyValue(key:String) -> NSArray{
    let ns = NSSortDescriptor.init(key: key, ascending: false)
    let aa = NSArray(object: ns)
    let arrResult = self.sortedArray(using: aa as! [NSSortDescriptor])
    return arrResult as NSArray
  }
}

use like this:

 let array=[
      [
        "msg":"Hi This is Jecky",
        "name":"Susheel",
        "sender":77,
        "timestamp":1464241769520,
        "username":"susheel",
        ],
      [
        "msg":"Dubai",
        "name":"Jecky",
        "sender":78,
        "timestamp":1464246547147,
        "username":"Jecky",
        ],
      [
        "msg":"How are you ?",
        "name":"Susheel",
        "sender":77,
        "timestamp":1464243480381,
        "username":"susheel",
        ],
      [
        "msg":"Aje dekhai nai",
        "name":"Jecky",
        "sender":78,
        "timestamp":1464244974198,
        "username":"Jecky",
        ],
      ]

    let a = NSArray.init(array: array)
    let filArray = a.ascendingArrayWithKeyValue(key: "timestamp")
    print(filArray)

Solution 4

To sort by property "timestamp"

array.sorted{$1["timestamp"] as? Long > $0["timestamp"] as? Long}

Solution 5

customArray.sortInPlace { 
  (element1, element2) -> Bool in
   return element1.someSortableField < element2.someSortableField
}

Check this out https://www.hackingwithswift.com/example-code/arrays/how-to-sort-an-array-using-sort

Share:
17,813
Jecky
Author by

Jecky

I am native iOS developer and interested to play with iOS codes daily and make crazy changes and things in to the code

Updated on June 17, 2022

Comments

  • Jecky
    Jecky almost 2 years
    [{
    msg = "Hi This is Jecky";
    name = Susheel;
    sender = 77;
    timestamp = 1464241769520;
    username = susheel;
    }, {
    msg = Dubai;
    name = Jecky;
    sender = 78;
    timestamp = 1464246547147;
    username = Jecky;
    }, {
    msg = "How are you ?";
    name = Susheel;
    sender = 77;
    timestamp = 1464243480381;
    username = susheel;
    }, {
    msg = "Aje dekhai nai";
    name = Jecky;
    sender = 78;
    timestamp = 1464244974198;
    username = Jecky;
    }]
    
    • I have an array like this. I want to sort this array using timestamp in swift 2.3 or latest version of swift. Can anyone help me for this ?
  • hansvb
    hansvb almost 8 years
    This does not look like JSON.
  • Ashok Varma
    Ashok Varma almost 8 years
    i was refering @fnc12 answer and taught it was json :p