2016-05-27 16 views
6
[{ 
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; 
}] 
  • Ho un array come questo. Voglio ordinare questo array usando il timestamp in Swift 2.3 o l'ultima versione di swift. Qualcuno può aiutarmi per questo?
+1

Ordine ascendente di cosa? Nome? Timestamp? Nome utente? – Thilo

+0

scusa, me lo sono dimenticato. È il timestamp – Jecky

+0

Controlla questa risposta: http://stackoverflow.com/a/24685377/3202193 –

risposta

7
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)") 
0

=> Innanzitutto, converti il ​​tuo JSON in Oggetti. (Controllare questo link per farlo: - http://roadfiresoftware.com/2015/10/how-to-parse-json-with-swift-2/)

=> Poi dichiarare la vostra matrice come matrice tipizzata in modo che si può chiamare metodi quando si scorrere:

var array : [yourObjectClassName] = [] 

=> Poi si può semplicemente ordinare la valore di:

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

L'esempio precedente ordina tutti gli array per nome. Se avete bisogno di ordinare da Timestamp è possibile cambiare il nome in timeStamp .etc

controllare questo link per ulteriori esempi di ordinamento: Swift how to sort array of custom objects by property value

+0

Questo non sembra JSON. – Thilo

+0

mi riferivo alla risposta di fnc12 e ho insegnato che era json: p –

4

Se il tuo array è mutabile puoi utente sortInPlace

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

e se non, è possibile creare un nuovo array di sorta, come suggerito da Kristijan (anche se senza bisogno di parentesi sulle chiusure finali):

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

Per ordinare in proprietà "timestamp"

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

È possibile ottenere questa funzionalità con estensione:

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

uso in questo modo:

012.351.641,061 mila
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)