@kingjs/linq.order-by-descending

Generates a sequence of elements in descending order according to a key.

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
04Jun 21, 2021Jul 20, 2018Minified + gzip package size for @kingjs/linq.order-by-descending in KB

Readme

@kingjs/linq.order-by-descending

Generates a sequence of elements in descending order according to a key.

Usage

Sort in descending order the numbers 1, 0, 2 in descending order like this:

var orderBy = require('@kingjs/linq.order-by-descending');
var sequence = require('@kingjs/enumerable.create');
var toArray = require('@kingjs/linq.to-array');

var numbers = sequence(1, 0, 2);

var result = orderByDescending.call(numbers);

toArray.call(result);

result:

[2, 1, 0]

Sort in descending order the same numbers as before, but now wrapped in objects, like this:

var orderBy = require('@kingjs/linq.order-by-descending');
var sequence = require('@kingjs/enumerable.create');
var toArray = require('@kingjs/linq.to-array');

var numbers = sequence({ value: 1 }, { value: 0 }, { value: 2 });
var keySelector = function(x) { return x.value; };

var result = orderByDescending.call(numbers, keySelector);

toArray.call(result);

result:

[{ value: 2 }, { value: 1 }, { value: 1 }]

Sort in descending order the numbers 1, 0, 2 and letters b, a so letters compare less than numbers like this:

var orderBy = require('@kingjs/linq.order-by');
var sequence = require('@kingjs/enumerable.create');
var toArray = require('@kingjs/linq.to-array');

var numbers = sequence(1, 0, 2, `b`, `a`);
var keySelector = null; // use default
var lessThan = function(l, r) {
  if (typeof l != typeof r)
    return typeof l == 'string';
  return l < r;
};

var result = orderBy.call(numbers, keySelector, lessThan);

toArray.call(result);

result:

['a', 'b', 0, 1, 2]

Sort Bob Smith, Alice Smith, and Chris King in descending order by last name then first name like this:

var orderBy = require('@kingjs/linq.order-by');
var sequence = require('@kingjs/enumerable.create');
var toArray = require('@kingjs/linq.to-array');

var people = sequence(
  { first: 'Bob', last: 'Smith' },
  { first: 'Alice', last: 'Smith' },
  { first: 'Chris', last: 'King' },
);

var lastSelector = function(x) { return x.last; }
var firstSelector = function(x) { return x.first; }
var lessThan = null; // use default

var sortedSequence = orderByDescending
  .call(people, lastSelector)
  .createOrderedEnumerable(firstSelector, lessThan, true);

toArray.call(sortedSequence);

result:

[
  { first: 'Bob', last: 'Smith' },
  { first: 'Alice', last: 'Smith' },
  { first: 'Chris', last: 'King' },
]

API

declare function orderByDescending(
  this: Enumerable, 
  keySelector?: (x) => any,
  lessThan?: (l, r) => boolean,
): SortedEnumerable

Interfaces

Parameters

Analytics

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.