如何按升序顺序对数组进行排序2.3

时间:2016-05-27 05:32:34

标签: ios iphone arrays swift sorting

[{
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中使用时间戳对这个数组进行排序。任何人都可以帮我吗?

6 个答案:

答案 0 :(得分: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)")

答案 1 :(得分:6)

如果您的阵列是可变的,您可以使用sortInPlace

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

如果没有,你可以从排序中创建一个新数组,就像Kristijan建议的那样(虽然在尾随闭包时不需要括号):

let newArray = yourArray.sort{$0.timestamp < $1.timestamp}

答案 2 :(得分:1)

customArray.sortInPlace { 
  (element1, element2) -> Bool in
   return element1.someSortableField < element2.someSortableField
}

检查一下 https://www.hackingwithswift.com/example-code/arrays/how-to-sort-an-array-using-sort

答案 3 :(得分:1)

按属性排序&#34;时间戳&#34;

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

答案 4 :(得分:1)

您可以使用扩展程序获取此功能:

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

像这样使用:

 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)

答案 5 :(得分:0)

=&GT;首先,将您的Json转换为对象。 (选中此链接即可: - http://roadfiresoftware.com/2015/10/how-to-parse-json-with-swift-2/

=&GT;然后将Array声明为类型化数组,以便在迭代时调用方法:

var array : [yourObjectClassName] = []

=&GT;然后您可以通过以下方式对值进行排序:

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

上面的示例按名称对所有数组进行排序。如果需要按timeStamp排序,可以将名称更改为timeStamp ..etc

查看此链接以获取更多排序示例:Swift how to sort array of custom objects by property value