ios – 如何按升序排序数组swift 2.3

前端之家收集整理的这篇文章主要介绍了ios – 如何按升序排序数组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;
}]

>我有这样的阵列.我想在swift 2.3或最新版本的swift中使用时间戳对这个数组进行排序.任何人都可以帮助我吗?

解决方法

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 ?","timestamp":1464243480381,[
            "msg":"Aje dekhai nai","timestamp":1464244974198,]
    print("array = \(array)")
    let sortedArray=array.sort { (obj1,obj2) -> Bool in
        return (obj1["timestamp"] as! Double) < (obj2["timestamp"] as! Double)
    }
    print("sortedArray = \(sortedArray)")

猜你在找的iOS相关文章