8
[{
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 ?
asked May 27, 2016 at 5:32
5
  • 1
    Ascending order of what? Name? Timestamp? Username? Commented May 27, 2016 at 5:33
  • sorry dude i forgot that. It's timestamp Commented May 27, 2016 at 5:35
  • Check this answer : stackoverflow.com/a/24685377/3202193 Commented May 27, 2016 at 5:37
  • This is an Array not NSArray so Sortdescriptor won't work here. Commented May 27, 2016 at 5:39
  • this is what i want exactly!!! Commented Oct 13, 2018 at 6:45

6 Answers 6

9
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)")
answered May 27, 2016 at 5:42
7

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}
answered May 27, 2016 at 5:43
2

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)
answered Aug 21, 2017 at 6:58
1
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

answered May 27, 2016 at 5:41
1

To sort by property "timestamp"

array.sorted{1ドル["timestamp"] as? Long > 0ドル["timestamp"] as? Long}
answered May 27, 2016 at 5:45
0

=> 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

answered May 27, 2016 at 5:41
2
  • This does not look like JSON. Commented May 27, 2016 at 5:44
  • i was refering @fnc12 answer and taught it was json :p Commented May 27, 2016 at 5:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.