This project is read-only.

orderBy()

The orderBy linq operator returns a new linq object that contains the results sorted in ascending order of the projected value given by the "keySelector" function. The orderBy operator will throw an exception if the keySelector is not given or is not a function.
var arr = [{id: 'a', pos: 5},
    {id: 'd', pos: 2},
    {id: 'b', pos: 4},
    {id: 'c', pos: 3},
    {id: 'e', pos: 1}];
	
// col1 = ['e', 'd', 'c', 'b', 'a']
var col1 = $linq(arr)
    .orderBy(function (x) { return x.pos; })
    .select(function (x) { return x.id; })
    .toArray();
	
// col2 = ['a', 'b', 'c', 'd', 'e']
var col2 = $linq(arr)
    .orderBy("x => x.id")
    .select("x => x.id")
    .toArray();
The orderBy operator can also be given an optional "comparer" function that is used to specify the ordering of the projected keys.
var arr = [{id: 1, lastname: 'Benson'},
    {id:4, lastname: 'MANSON'},
    {id:3, lastname: 'Jones'},
    {id:2, lastname: 'Hardcastle'},
    {id:5, lastname: 'russel'}];
	
// col = [1, 2, 3, 4, 5]
var col = $linq(arr)
    .orderBy("x => x.lastname",        // keySelector
        linq.caseInsensitiveComparer)  // case-insensitive comparer
    .select("x => x.id")
    .toArray();

orderByDescending()

The orderByDescending linq operator is similar to the orderBy operator, except that it sorts the results in descending order.
var arr = [{id: 'a', pos: 5},
    {id: 'd', pos: 2},
    {id: 'b', pos: 4},
    {id: 'c', pos: 3},
    {id: 'e', pos: 1}];
	
// col1 = ['a', 'b', 'c', 'd', 'e']
var col1 = $linq(arr)
    .orderByDescending("x => x.pos")
    .select("x => x.id")
    .toArray();

thenBy()

The thenBy linq operator can be used immediately after the orderBy (or orderByDescending) operator to provide a second level of sorting (i.e., sorting that is applied when the orderBy compares two elements as equal) in ascending order. The thenBy operator can also be used after another thenBy (or thenByDescending) operator to provide further levels of sorting. If the thenBy operator is used following any other linq operator (or by itself), an exception will be thrown.
var arr = [{id: 1, year: 2000, month: 5, day: 22},
    {id: 4, year: 2002, month: 2, day: 10},
    {id: 3, year: 2002, month: 2, day: 1},
    {id: 2, year: 2002, month: 1, day: 1},
    {id: 5, year: 2012, month: 8, day: 22}];
    
// col = [1, 2, 3, 4, 5]
var col = $linq(arr)
    .orderBy("x => x.year")
    .thenBy("x => x.month")
    .thenBy("x => x.day")
    .select("x => x.id")
    .toArray();
As with the orderBy operator, the thenBy operator can take a comparer function to specify the ordering of the projected keys.

thenByDescending()

The thenByDescending linq operator is similar to the thenBy operator, except that it sorts the results in descending order.

Last edited Dec 19, 2013 at 3:51 AM by battousai999, version 11