Function

Module: @ember/utils
v
Object
First value to compare
w
Object
Second value to compare
returns
Number
-1 if v < w, 0 if v = w and 1 if v > w.

Compares two javascript values and returns:

  • -1 if the first is smaller than the second,

  • 0 if both are equal,

  • 1 if the first is greater than the second.

    1
    2
    3
    4
    5
    
    import { compare } from '@ember/utils';
    
    compare('hello', 'hello');  // 0
    compare('abc', 'dfg');      // -1
    compare(2, 1);              // 1

If the types of the two objects are different precedence occurs in the following order, with types earlier in the list considered < types later in the list:

  • undefined

  • null

  • boolean

  • number

  • string

  • array

  • object

  • instance

  • function

  • class

  • date

    1
    2
    3
    4
    
    import { compare } from '@ember/utils';
    
    compare('hello', 50);       // 1
    compare(50, 'hello');       // -1