0

Can someone help me to sort list of my custom objects by multiple parameters.
I want to sort it by date1 and date2 and optionally b all three properties but whatever I try it fails :(
My object:

class MyObject : NSManagedObject{
   @NSManaged var date1:NSDate
   @NSManaged var date2:NSDate
   @NSManaged var key:Int
}

This is my failed tries to sort list of those objects:

var sortedArray1 = sorted(unorderedList, { (o1: MyObject, o2: MyObject) -> Bool in{
            return o1.date1 == o2.date1 ? (o1.date2 < o2.date2) : (o1.date1 < o2.date1)
        }

        var sortedArray2 = sorted(unorderedList, { (o1: MyObject, o2: MyObject) -> Bool in
            return (o1.date1.compare(o2.date1) == NSComparisonResult.OrderedAscending) == (o1.date1.compare(o2.date1) == NSComparisonResult.OrderedAscending) ? (o1.date2.compare(o2.date2) == NSComparisonResult.OrderedAscending) : (o1.date1.compare(o2.date1) == NSComparisonResult.OrderedAscending))
        });
4
  • Could you please specify in details the criteria for sorting. Commented Mar 30, 2015 at 7:40
  • date1 and date2 are date with time. This is some from and to properties. I need to sort list by those two properties to get something like like: 04:00to05:00, 04:00to05:30 etc. And key is simple 1,2,3... but that key can also be nil so primary sorting should be by date properties. Commented Mar 30, 2015 at 7:52
  • So sort by date and if dates are equal get into account the keys. For dates you could not use < or > operators as these are not provided by Swift except you have defined it yourself. Same for == or != operators. If you have not and wants to do it, I will request you to look at this framework github.com/abdullah-chhatra/iDate. Commented Mar 30, 2015 at 7:55
  • Ok if I want to avoid sorting by key so just by dates how can I sort this? Commented Mar 30, 2015 at 7:57

1 Answer 1

1

Try this out and let me know:

unorderedList.sorted({
    if $0.date1 < $1.date1 {
        return true
    } else if $0.date1 == $date2 {
        return $0.date2 < $1.date2
    }
    return false
})

public func == (left: NSDate, right: NSDate) -> Bool {
    return left.isEqualToDate(right)
}

public func != (left: NSDate, right: NSDate) -> Bool {
    return !left.isEqualToDate(right)
}

public func > (left: NSDate, right: NSDate) -> Bool {
    return left.compare(right) == NSComparisonResult.OrderedDescending
}

public func < (left: NSDate, right: NSDate) -> Bool {
    return left.compare(right) == NSComparisonResult.OrderedAscending
}
Sign up to request clarification or add additional context in comments.

4 Comments

I get: NSDate is not a subtype of NSString. Where should I put those functions?
Put those in any file of your project outside of any class.
Results are wrong: 01:00to04:00, 02:00to04:00, 01:00to04:00, 01:00to04:00, 03:00to04:00
Try the new code. You could try to manipulate the conditions to make it work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.