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
1
2
3
4
5
6
import 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
1
2
3
4
5
6
7
8
import 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 elements
Console.log(arr.join("\n"));
Custom Sort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import 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