compare

Summary

Compares the Enumeration object to another Enumeration object.

Usage

public Comparison compare(Enumeration object)

Returns

A System.Comparison enumeration result representing whether the specified Enumeration comparison object is less than, equal to, or greater than the current Enumeration object.

Parameters

object

The Enumeration object to compare to.

Description

Compares the Enumeration object to another Enumeration 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
7
8
import System;
 
enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
  
Days d1 = Days.MONDAY;
Days d2 = Days.MONDAY;
Comparison result = d1.compare(d2);
Console.log(result == Comparison.EQUAL);
Custom Sort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import System;
import System.Assert;
 
enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
 
Days[] arr = [ Days.THURSDAY, Days.MONDAY, Days.SATURDAY ];
Console.log("BEFORE SORTING: " + arr.join(", "));
arr.sort(Comparison(Days a, Days b) {
    if (a < b) {
        return Comparison.LESS_THAN;
    }
    else if (a > b) {
        return Comparison.GREATER_THAN;
    }
    else {
        assert(a == b);
        return Comparison.EQUAL;
    }
});
Console.log("AFTER SORTING:  " + arr.join(", "));

Share

HTML | BBCode | Direct Link