thenByDesc(keySelector[, comparer, arg])

Performs a subsequent ordering of the elements in 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 each 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 according to a key.

Examples

var fruits = [ "apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE", "apPLE" ];

var comparer = function(a, b) {
	a = a.toLowerCase();
	b = b.toLowerCase();
	
	return (a > b ? 1 : (a < b ? -1 : 0));
};

// Sort the strings first ascending by their length and 
// then descending using a custom case insensitive comparer.
var query = $from(fruits)
    .orderBy("$length")
    .thenByDesc("$", comparer);

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

/*
This code produces the following output:

apPLe
apple
APple
apPLE
orange
ORANGE
baNanA
BAnana
*/

Last edited May 18, 2012 at 10:17 AM by suckgamoni, version 2

Comments

No comments yet.