compare

Summary

Compares the Double object to another Double object.

Usage

public final Comparison compare(Double object)

Returns

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

Parameters

object

The Double object to compare to.

Description

Compares the Double object to another Double 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;
 
double b = 100d;
Comparison result = b.compare(100d);
Console.log(result == Comparison.EQUAL);
Sorting
1
2
3
4
5
import System;
 
double[] arr = [ 100d, 50d, 200d, 40d ];
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;
 
double[] arr = [ 100d, 50d, 200d, 40d ];
arr.sort(Comparison(double a, double 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