Jecky
Jecky

Reputation: 478

How to Sort an array in an ascending order swift 2.3

[{
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;
}]

Upvotes: 8

Views: 21263

Answers (6)

Brijesh Shiroya
Brijesh Shiroya

Reputation: 3383

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)

Upvotes: 2

fnc12
fnc12

Reputation: 2237

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)")

Upvotes: 9

SaRaVaNaN DM
SaRaVaNaN DM

Reputation: 4410

To sort by property "timestamp"

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

Upvotes: 1

T. Benjamin Larsen
T. Benjamin Larsen

Reputation: 6393

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}

Upvotes: 7

Nagarjun
Nagarjun

Reputation: 6887

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

Upvotes: 1

Ashok Varma
Ashok Varma

Reputation: 3539

=> First, convert your Json to Objects. (check this link to do that :- http://roadfiresoftware.com/2015/10/how-to-parse-json-with-swift-2/ )

=> Then declare your Array as a typed array so that you can call methods when you iterate:

var array : [yourObjectClassName] = []

=> Then you can simply sort the value by :

array.sort({ $0.name > $1.name })

The above example sorts all the arrays by name. If you need to sort by timeStamp you can change the name to timeStamp ..etc

Check this link for more sorting examples : Swift how to sort array of custom objects by property value

Upvotes: 0

Related Questions