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

1
  • Try using NSPredicate Commented Feb 18, 2016 at 8:53

2 Answers 2

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)
}
Sign up to request clarification or add additional context in comments.

Comments

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)])

4 Comments

this dont work nsmtablearraydont have sortInPlace
You can cast your NSMutableArray in swift array in order to use sortInPlace
@KhaledAld I added example for NSMutableArray.
i'ts work good thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.