orderByDesc(keySelector[, comparer, arg])

suckgamoni edited this page May 15, 2013 · 1 revision

Sorts the elements of a sequence in descending order by using a specified comparer.

Syntax

Parameters

keySelector

Type: function(value, key, arg)
A function to extract a key from an element.

comparer

Type: function(value1, value2, arg): number
An comparer to compare keys. Should return 0 if value1 == value2, negative value if value1 < value2, positive value if value1 > value2.

arg

An external argument.

Return Value

An iterable object whose elements are sorted in descending order according to a key.


Example

var decimals = [ 6.2, 8.3, 0.5, 1.3, 6.3, 9.7 ];

function comparer(v1, v2) {
	var f1 = v1 - Math.floor(v1);
	var f2 = v2 - Math.floor(v2);
	
	if (f1 == f2) {
		return 0;
	}
	else if (f1 > f2) {
		return 1;
	}
	else {
		return -1;
	}
}

var query = from(decimals).orderByDesc(null, comparer);

query.each("console.log($)");

/*
 This code produces the following output:

 9.7
 0.5
 8.3
 1.3
 6.3
 6.2
*/