-1

i have nsmutablearray with objects

user_id , username , user_Last_Update_Info

and i want to sort this array by user_Last_Update_Info

how to do it ??

Khaled Ald
  • 83
  • 2
  • 11

2 Answers2

3

This should help:

    // Example for Swift Array
    myArray.sortInPlace {
        obj1 , obj2 in
        dic1.user_Last_Update_Info < dic2.user_Last_Update_Info
    }

// Example for Swift NSMutableArray
// newArr is an optional [AnyObject], you should cast it to what you expect
let newArr = myArray.sortedArrayUsingDescriptors([NSSortDescriptor(key: "user_Last_Update_Info", ascending: true)])
Greg
  • 25,317
  • 6
  • 53
  • 62
3

Try this:

arrayToSort.sortUsingComparator{
    (obj1:AnyObject!, obj2:AnyObject!) -> NSComparisonResult in
    var dateStr1 = obj1["user_Last_Update_Info"] as NSString
    var date1: NSDate = dateFormatter.dateFromString(dateStr1)!
    var dateStr2 = obj2["user_Last_Update_Info"] as NSString
    var date2: NSDate = dateFormatter.dateFromString(dateStr2)!
    return date2.compare(date1)
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Rashad
  • 11,057
  • 4
  • 45
  • 73