thenByDesc(keySelector[, comparer, arg])

suckgamoni edited this page May 15, 2013 · 1 revision

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 iterable object whose elements are sorted according to a key.


Example

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.
from(fruits)
    .orderBy("$length")
    .thenByDesc("$", comparer);
    .each("console.log($)");

/*
This code produces the following output:

apPLe
apple
APple
apPLE
orange
ORANGE
baNanA
BAnana
*/