Design & Download
Your JS Library
in Seconds!!!

Snippet: compare() & sort()

Written by Christopher West (cwest) on September 03, 2016.
Compare primitive values and sort arrays.
function compare(a, b) {
  return a !== b ? a > b ? 1 : -1 : 0;
}

function sort(arr, opt_comparer, opt_valueProcessor) {
  opt_comparer = opt_comparer || compare;
  var arrPre = slice(arr);
  if (opt_valueProcessor) {
    for (var arr2 = [], l = arrPre.length, i = l; i--; ) {
      arrPre[i] = { i: i, v: opt_valueProcessor(arrPre[i]) };
    }
    arrPre.sort(function(a, b) {
      return opt_comparer.call(this, a.v, b.v);
    });
    ;
    for (i = 0; i < l; i++) {
      arr2[i] = arr[arrPre[i].i];
    }
    return arr2;
  }
  return slice(arr).sort(opt_comparer);
}

compare(...) API Documentation

Description

Compares one value to another by testing for strict-equality and then if a value is greater than the other. This is mainly useful to provide a very primitive comparison function for the sort() function.

Parameters

  1. value1 {*}:
    The first value to be compared to value2.
  2. value2 {*}:
    The second value to be compared to value1

Returns

Returns 0 if value1 is strictly equal to value2. Returns 1 if value1 is greater than value2. In all other cases -1 is returned.

Examples

console.log(YourJS.compare('', '3'));    // -> -1
console.log(YourJS.compare('4', ''));    // -> 1
console.log(YourJS.compare('5', '5'));   // -> 0
console.log(YourJS.compare('a', 'A'));   // -> 1
console.log(YourJS.compare('12', '4'));  // -> -1
console.log(YourJS.compare('1', ['1'])); // -> -1

sort(...) API Documentation

Description

Sorts a shallow-copy of an array.

Parameters

  1. array {Array|Arguments}:
    The array (or arguments object) to be copied and sorted.
  2. opt_comparer {function(value1, value2): boolean}:
    Optional. Defaults to compare(). The function to be used to compare each value in the array and which will thusly determine the order of the values in the array. The function will be called with 2 arguments. The 1st argument will be a value pulled from one position in the array while the 2nd will be a value pulled from another position in the array. When this function returns a negative value that will cause the 1st value to be placed before the 2nd value in the returned array. When this function returns a positive value that will cause the 1st value to be placed after the 2nd value in the returned array.
  3. opt_valueProcessor {Function}:
    Optional. If specified this function will be used to process each value in array and the values returned from this function will be sent to the comparer function. NOTE: Although the values in the array will be processed by this function, the returned array will contain the original values found in array.

Returns

Returns a shallow-copy of array sorted.

Examples

var arr = [
  { size: 4, name: 'C' },
  { size: 2, name: 'a' },
  { size: 1, name: 'B' }
];

console.log(sort(arr, function(a, b) {
  return a.name < b.name
    ? -1
    : a.name == b.name
      ? 0
      : 1; 
}));  // -> [ { size: 1, name: "B" }, { size: 4, name: "C" }, { size: 2, name: "a" } ]

console.log(sort(arr, function(a, b) {
  return a.name.toUpperCase() < b.name.toUpperCase()
    ? -1
    : a.name.toUpperCase() == b.name.toUpperCase()
      ? 0
      : 1; 
}));  // -> [ { size: 2, name: "a" }, { size: 1, name: "B" }, { size: 4, name: "C" } ]

console.log(sort(arr, undefined, function(item) {
  return item.name.toUpperCase(); 
}));  // -> [ { size: 2, name: "a" }, { size: 1, name: "B" }, { size: 4, name: "C" } ]