Multisort
Sort an array using any number of separate, ranked criteria
Install / Use
/learn @peterkhayes/MultisortREADME
Multitest
A small library for sorting arrays by multiple criteria.
Installation
npm install multisort --save
bower install multisort --save
Basic Usage
var input = [
{firstName: "Kate", lastName: "Bush"},
{firstName: "George", lastName: "Bush", suffix: "Junior"},
{firstName: "George", lastName: "Orwell"},
{firstName: "George", lastName: "Bush", suffix: "Senior"},
];
var criteria = [
'firstName',
'~lastName.length',
'suffix.charAt(1)'
];
multisort(inputArray, criteria)
// input is now sorted by firstName (ascending), then lastName.length (descending),
// and finally suffix.charAt, called with 1 as the argument:
// [
// {firstName: "George", lastName: "Orwell"},
// {firstName: "George", lastName: "Bush", suffix: "Senior"},
// {firstName: "George", lastName: "Bush", suffix: "Junior"},
// {firstName: "Kate", lastName: "Bush"},
// ];
var input = [8, 7, 6, 5, 4, 3, 2, 1];
var criteria = [
function(a) {return a % 2},
function(a) {return a % 3},
function(a) {return a}
];
multisort(input, criteria);
// input is now:
// [6, 4, 2, 8, 3, 1, 7, 5]
Partial Application
// Passing a single argument makes a sorting function that can then be applied to lists.
var sortByMod2AndMod3 = multisort([
function(a) {return a % 2},
function(a) {return a % 3},
function(a) {return a}
]);
var input1 = [8, 7, 6, 5, 4, 3, 2, 1];
sortByMod2AndMod3(input1);
// input1 is [6, 4, 2, 8, 3, 1, 7, 5]
var input2 = [1, 5, 10, 25, 50, 100];
sortByMod2AndMod3(input2);
// input2 is [10, 100, 50, 1, 25, 5]
Features
Criteria types:
- Function: Applies the given function to each element, and the return values are sorted in ascending order, using < and >.
- String: Picks out the property indicated by the string, and sorts in ascending order, using < and >. Nested properties can be accessed with dot syntax, as in "property.subproperty.subsubproperty"
- !String or ~String: As above, but sorted in descending order. This works with the functional and existential operators below.
- String(arg1, arg2...): As above, but calls the indicated property as a function with the given arguments. Arguments are split by commas, trimmed, and parsed with JSON.parse.
- String?: As above, tests whether the property is null or undefined. Items without the property are sorted before items with it.
Tests
npm test
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
Release History
- 0.1.0 Initial release
Related Skills
node-connect
347.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
