[{
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 ?
-
1Ascending order of what? Name? Timestamp? Username?Thilo– Thilo05/27/2016 05:33:49Commented May 27, 2016 at 5:33
-
sorry dude i forgot that. It's timestampJecky– Jecky05/27/2016 05:35:37Commented May 27, 2016 at 5:35
-
Check this answer : stackoverflow.com/a/24685377/3202193Ashish Kakkad– Ashish Kakkad05/27/2016 05:37:23Commented May 27, 2016 at 5:37
-
This is an Array not NSArray so Sortdescriptor won't work here.Jecky– Jecky05/27/2016 05:39:07Commented May 27, 2016 at 5:39
-
this is what i want exactly!!!guru– guru10/13/2018 06:45:22Commented Oct 13, 2018 at 6:45
6 Answers 6
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)")
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}
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)
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
To sort by property "timestamp"
array.sorted{1ドル["timestamp"] as? Long > 0ドル["timestamp"] as? Long}
=> 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
-
-
i was refering @fnc12 answer and taught it was json :pAshok Varma– Ashok Varma05/27/2016 05:46:30Commented May 27, 2016 at 5:46