How to sort an array in 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; }] 
  • I have such an array. I want to sort this array using timestamp in swift 2.3 or the latest version of Quick. Can someone help me with this?
+6
source share
6 answers
 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)") 
+7
source

If your array is modified, you can use sortInPlace

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

and if not, you can create a new array from sorting, for example, suggested by Kristian (although there is no need for parentheses when closing closures):

 let newArray = yourArray.sort{$0.timestamp < $1.timestamp} 
+4
source
 customArray.sortInPlace { (element1, element2) -> Bool in return element1.someSortableField < element2.someSortableField } 

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

+1
source

Sort by property "timestamp"

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

You can get this functionality with the 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 the following:

  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) 
+1
source

=> First convert Json to objects. (check this link: - 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 on repeat:

 var array : [yourObjectClassName] = [] 

=> Then you can just sort the value by:

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

In the above example, all arrays are sorted by name. If you need to sort by timeStamp, you can change the name to timeStamp..etc

See this link for more detailed sorting examples: Swift, how to sort an array of custom objects by property value

0
source

All Articles