This project is read-only.

orderBy(keySelector[, comparer, arg])

Sorts the elements of a sequence in ascending 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 according to a key.

Examples

var unsortedArray = [ "three", "six", "nine", "twelve", "fifteen", "eighteen" ];

 // Sort the elements of the array alphabetically.
var sortedArray = $from(unsortedArray).orderBy("$");

document.writeln("Array elements in alphabetical order:");
sortedArray.each("document.writeln($)");

// Change the lambda expression to sort by the length of each string.
sortedArray = $from(unsortedArray).orderBy("$length");

document.writeln("\nArray elements sorted by the lengths of the strings:");
sortedArray.each("document.writeln($)");

// Output:
// Array elements in alphabetical order:
// eighteen
// fifteen
// nine
// six
// three
// twelve

// Array elements sorted by the lengths of the strings:
// six
// nine
// three
// twelve
// fifteen
// eighteen

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

Comments

No comments yet.