compare

Summary

Compares the Boolean object to another Boolean object.

Usage

public final Comparison compare(Boolean object)

Returns

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

Parameters

object

The Boolean object to compare to.

Description

Compares the Boolean object to another Boolean 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
import System;
 
bool b = true;
Comparison result = b.compare(true);
Console.log(result == Comparison.EQUAL);
Sorting
1
2
3
4
5
import System;
 
bool[] arr = [ true, false, true, false ];
arr.sort(); // 'System.Array<T>.sort' uses 'compare' to sort elements
Console.log(arr.join(", "));
Custom Sort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import System;
import System.Assert;
 
bool[] arr = [ true, false, true, false ];
arr.sort(Comparison(bool a, bool 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(arr.join(", "));

Share

HTML | BBCode | Direct Link