orderByDesc(keySelector[, comparer, arg])

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 iteratable object whose elements are sorted in descending order according to a key.

Examples

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("$", comparer);

query.each("document.writeln($)");

/*
 This code produces the following output:

 9.7
 0.5
 8.3
 1.3
 6.3
 6.2
*/

Last edited May 17, 2012 at 1:29 PM by suckgamoni, version 1

Comments

No comments yet.