compare Summary Compares the Date object to another Date object. Usage public final Comparison compare(Date object) Returns A System.Comparison enumeration result representing whether the specified Date comparison object is less than, equal to, or greater than the current Date object. Parameters object The Date object to compare to. Description Compares the Date object to another Date object. Returns one of the following System.Comparison enumeration values: System.Comparison.LESS_THAN System.Comparison.EQUAL System.Comparison.GREATER_THAN Examples Basic Usage 123456import System; Date d1 = new Date(2011, Date.Month.FEB, 10);Date d2 = new Date(2011, Date.Month.FEB, 10);Comparison result = d1.compare(d2);Console.log(result == Comparison.EQUAL); Sorting 12345678import System; Date[] arr = [ new Date(2011, Date.Month.FEB, 10), new Date(2011, Date.Month.FEB, 11)];arr.sort(); // 'System.Array<T>.sort' uses 'compare' to sort elementsConsole.log(arr.join("\n")); Custom Sort 123456789101112131415161718192021import System;import System.Assert; Date[] arr = [ new Date(2011, Date.Month.FEB, 10), new Date(2011, Date.Month.FEB, 11), new Date(2011, Date.Month.FEB, 12)];arr.sort(Comparison(Date a, Date b) { if (a.getTime() < b.getTime()) { return Comparison.LESS_THAN; } else if (a.getTime() > b.getTime()) { return Comparison.GREATER_THAN; } else { assert(a.getTime() == b.getTime()); return Comparison.EQUAL; }});Console.log(arr.join("\n")); Share HTML | BBCode | Direct Link