Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-sort

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-sort - npm Package Compare versions

Comparing version 2.2.0 to 3.0.0

CHANGELOG.md

70

dist/sort.d.ts

@@ -7,2 +7,3 @@ declare type IOrder = 1 | -1;

comparer?: IComparer;
inPlaceSorting?: boolean;
}

@@ -20,5 +21,5 @@ export interface ISortByFunction<T> {

export declare type ISortByObjectSorter<T> = ISortByAscSorter<T> | ISortByDescSorter<T>;
declare function createSortInstance(opts: ISortInstanceOptions): <T>(ctx: T[]) => {
export declare const createNewSortInstance: (opts: ISortInstanceOptions) => <T>(_ctx: T[]) => {
/**
* Sort array in ascending order. Mutates provided array by sorting it.
* Sort array in ascending order.
* @example

@@ -32,5 +33,5 @@ * sort([3, 1, 4]).asc();

*/
asc(sortBy?: ISortByFunction<T> | keyof T | (keyof T | ISortByFunction<T>)[] | ISortBy<T>[]): T[];
asc(sortBy?: ISortBy<T> | ISortBy<T>[]): T[];
/**
* Sort array in descending order. Mutates provided array by sorting it.
* Sort array in descending order.
* @example

@@ -44,6 +45,6 @@ * sort([3, 1, 4]).desc();

*/
desc(sortBy?: ISortByFunction<T> | keyof T | (keyof T | ISortByFunction<T>)[] | ISortBy<T>[]): T[];
desc(sortBy?: ISortBy<T> | ISortBy<T>[]): T[];
/**
* Sort array in ascending or descending order. It allows sorting on multiple props
* in different order for each of them. Mutates provided array by sorting it.
* in different order for each of them.
* @example

@@ -55,7 +56,7 @@ * sort(users).by([

*/
by(sortBy: ISortByAscSorter<T> | ISortByDescSorter<T> | ISortByObjectSorter<T>[]): T[];
by(sortBy: ISortByObjectSorter<T> | ISortByObjectSorter<T>[]): T[];
};
declare const defaultSort: <T>(ctx: T[]) => {
export declare const sort: <T>(_ctx: T[]) => {
/**
* Sort array in ascending order. Mutates provided array by sorting it.
* Sort array in ascending order.
* @example

@@ -69,5 +70,5 @@ * sort([3, 1, 4]).asc();

*/
asc(sortBy?: ISortByFunction<T> | keyof T | (keyof T | ISortByFunction<T>)[] | ISortBy<T>[]): T[];
asc(sortBy?: ISortBy<T> | ISortBy<T>[]): T[];
/**
* Sort array in descending order. Mutates provided array by sorting it.
* Sort array in descending order.
* @example

@@ -81,6 +82,6 @@ * sort([3, 1, 4]).desc();

*/
desc(sortBy?: ISortByFunction<T> | keyof T | (keyof T | ISortByFunction<T>)[] | ISortBy<T>[]): T[];
desc(sortBy?: ISortBy<T> | ISortBy<T>[]): T[];
/**
* Sort array in ascending or descending order. It allows sorting on multiple props
* in different order for each of them. Mutates provided array by sorting it.
* in different order for each of them.
* @example

@@ -92,9 +93,38 @@ * sort(users).by([

*/
by(sortBy: ISortByAscSorter<T> | ISortByDescSorter<T> | ISortByObjectSorter<T>[]): T[];
by(sortBy: ISortByObjectSorter<T> | ISortByObjectSorter<T>[]): T[];
};
declare type ISortFunction = typeof defaultSort;
interface ISortExport extends ISortFunction {
createNewInstance: typeof createSortInstance;
}
declare const _default: ISortExport;
export default _default;
export declare const inPlaceSort: <T>(_ctx: T[]) => {
/**
* Sort array in ascending order.
* @example
* sort([3, 1, 4]).asc();
* sort(users).asc(u => u.firstName);
* sort(users).asc([
* U => u.firstName
* u => u.lastName,
* ]);
*/
asc(sortBy?: ISortBy<T> | ISortBy<T>[]): T[];
/**
* Sort array in descending order.
* @example
* sort([3, 1, 4]).desc();
* sort(users).desc(u => u.firstName);
* sort(users).desc([
* U => u.firstName
* u => u.lastName,
* ]);
*/
desc(sortBy?: ISortBy<T> | ISortBy<T>[]): T[];
/**
* Sort array in ascending or descending order. It allows sorting on multiple props
* in different order for each of them.
* @example
* sort(users).by([
* { asc: u => u.score }
* { desc: u => u.age }
* ]);
*/
by(sortBy: ISortByObjectSorter<T> | ISortByObjectSorter<T>[]): T[];
};
export {};

@@ -66,3 +66,3 @@ // >>> INTERFACES <<<

}
var sort = function (order, ctx, sortBy, comparer) {
var sortArray = function (order, ctx, sortBy, comparer) {
var _a;

@@ -79,8 +79,11 @@ if (!Array.isArray(ctx)) {

// >>> Public <<<
function createSortInstance(opts) {
var createNewSortInstance = function (opts) {
var comparer = castComparer(opts.comparer);
return function (ctx) {
return function (_ctx) {
var ctx = Array.isArray(_ctx) && !opts.inPlaceSorting
? _ctx.slice()
: _ctx;
return {
/**
* Sort array in ascending order. Mutates provided array by sorting it.
* Sort array in ascending order.
* @example

@@ -95,6 +98,6 @@ * sort([3, 1, 4]).asc();

asc: function (sortBy) {
return sort(1, ctx, sortBy, comparer);
return sortArray(1, ctx, sortBy, comparer);
},
/**
* Sort array in descending order. Mutates provided array by sorting it.
* Sort array in descending order.
* @example

@@ -109,7 +112,7 @@ * sort([3, 1, 4]).desc();

desc: function (sortBy) {
return sort(-1, ctx, sortBy, comparer);
return sortArray(-1, ctx, sortBy, comparer);
},
/**
* Sort array in ascending or descending order. It allows sorting on multiple props
* in different order for each of them. Mutates provided array by sorting it.
* in different order for each of them.
* @example

@@ -122,23 +125,26 @@ * sort(users).by([

by: function (sortBy) {
return sort(1, ctx, sortBy, comparer);
return sortArray(1, ctx, sortBy, comparer);
},
};
};
}
var defaultSort = createSortInstance({
comparer: function (a, b, order) {
if (a == null)
return order;
if (b == null)
return -order;
if (a < b)
return -1;
if (a === b)
return 0;
return 1;
},
};
var defaultComparer = function (a, b, order) {
if (a == null)
return order;
if (b == null)
return -order;
if (a < b)
return -1;
if (a === b)
return 0;
return 1;
};
var sort = createNewSortInstance({
comparer: defaultComparer,
});
// Attach createNewInstance to sort function
defaultSort['createNewInstance'] = createSortInstance;
var inPlaceSort = createNewSortInstance({
comparer: defaultComparer,
inPlaceSorting: true,
});
export default defaultSort;
export { createNewSortInstance, inPlaceSort, sort };
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global['fast-sort'] = factory());
}(this, (function () { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['fast-sort'] = {}));
}(this, (function (exports) { 'use strict';

@@ -72,3 +72,3 @@ // >>> INTERFACES <<<

}
var sort = function (order, ctx, sortBy, comparer) {
var sortArray = function (order, ctx, sortBy, comparer) {
var _a;

@@ -85,8 +85,11 @@ if (!Array.isArray(ctx)) {

// >>> Public <<<
function createSortInstance(opts) {
var createNewSortInstance = function (opts) {
var comparer = castComparer(opts.comparer);
return function (ctx) {
return function (_ctx) {
var ctx = Array.isArray(_ctx) && !opts.inPlaceSorting
? _ctx.slice()
: _ctx;
return {
/**
* Sort array in ascending order. Mutates provided array by sorting it.
* Sort array in ascending order.
* @example

@@ -101,6 +104,6 @@ * sort([3, 1, 4]).asc();

asc: function (sortBy) {
return sort(1, ctx, sortBy, comparer);
return sortArray(1, ctx, sortBy, comparer);
},
/**
* Sort array in descending order. Mutates provided array by sorting it.
* Sort array in descending order.
* @example

@@ -115,7 +118,7 @@ * sort([3, 1, 4]).desc();

desc: function (sortBy) {
return sort(-1, ctx, sortBy, comparer);
return sortArray(-1, ctx, sortBy, comparer);
},
/**
* Sort array in ascending or descending order. It allows sorting on multiple props
* in different order for each of them. Mutates provided array by sorting it.
* in different order for each of them.
* @example

@@ -128,25 +131,32 @@ * sort(users).by([

by: function (sortBy) {
return sort(1, ctx, sortBy, comparer);
return sortArray(1, ctx, sortBy, comparer);
},
};
};
}
var defaultSort = createSortInstance({
comparer: function (a, b, order) {
if (a == null)
return order;
if (b == null)
return -order;
if (a < b)
return -1;
if (a === b)
return 0;
return 1;
},
};
var defaultComparer = function (a, b, order) {
if (a == null)
return order;
if (b == null)
return -order;
if (a < b)
return -1;
if (a === b)
return 0;
return 1;
};
var sort = createNewSortInstance({
comparer: defaultComparer,
});
// Attach createNewInstance to sort function
defaultSort['createNewInstance'] = createSortInstance;
var inPlaceSort = createNewSortInstance({
comparer: defaultComparer,
inPlaceSorting: true,
});
return defaultSort;
exports.createNewSortInstance = createNewSortInstance;
exports.inPlaceSort = inPlaceSort;
exports.sort = sort;
Object.defineProperty(exports, '__esModule', { value: true });
})));

@@ -1,1 +0,1 @@

!function(r,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(r=r||self)["fast-sort"]=n()}(this,function(){"use strict";function i(e){return function(r,n,t){return e(r,n,t)*t}}var c=function(r,n){if(r)throw Error("Invalid sort config: "+n)},y=function(r){var n=r||{},t=n.asc,e=n.desc,o=t?1:-1,u=t||e;return c(!u,"Expected `asc` or `desc` property"),c(t&&e,"Ambiguous object with `asc` and `desc` config properties"),{order:o,sortBy:u,comparer:r.comparer&&i(r.comparer)}},f=function(d){return function r(n,t,e,o,u,i,c){var f,a;if("string"==typeof n)f=i[n],a=c[n];else{if("function"!=typeof n){var s=y(n);return r(s.sortBy,t,e,s.order,s.comparer||d,i,c)}f=n(i),a=n(c)}var p=u(f,a,o);return(0===p||null==f&&null==a)&&t.length>e?r(t[e],t,e+1,o,u,i,c):p}};function e(r,n,t,e){return Array.isArray(n)?(Array.isArray(t)&&t.length<2&&(t=t[0]),n.sort(function r(t,e,o){if(void 0===t||!0===t)return function(r,n){return e(r,n,o)};if("string"==typeof t)return c(t.includes("."),"String syntax not allowed for nested properties."),function(r,n){return e(r[t],n[t],o)};if("function"==typeof t)return function(r,n){return e(t(r),t(n),o)};if(Array.isArray(t)){var u=f(e);return function(r,n){return u(t[0],t,1,o,e,r,n)}}var n=y(t);return r(n.sortBy,n.comparer||e,n.order)}(t,e,r))):n}function r(r){var t=i(r.comparer);return function(n){return{asc:function(r){return e(1,n,r,t)},desc:function(r){return e(-1,n,r,t)},by:function(r){return e(1,n,r,t)}}}}var n=r({comparer:function(r,n,t){return null==r?t:null==n?-t:r<n?-1:r===n?0:1}});return n.createNewInstance=r,n});
!function(r,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((r="undefined"!=typeof globalThis?globalThis:r||self)["fast-sort"]={})}(this,function(r){"use strict";function u(t){return function(r,n,e){return t(r,n,e)*e}}var c=function(r,n){if(r)throw Error("Invalid sort config: "+n)},d=function(r){var n=r||{},e=n.asc,t=n.desc,o=e?1:-1,i=e||t;return c(!i,"Expected `asc` or `desc` property"),c(e&&t,"Ambiguous object with `asc` and `desc` config properties"),{order:o,sortBy:i,comparer:r.comparer&&u(r.comparer)}},f=function(p){return function r(n,e,t,o,i,u,c){var f,a;if("string"==typeof n)f=u[n],a=c[n];else{if("function"!=typeof n){var s=d(n);return r(s.sortBy,e,t,s.order,s.comparer||p,u,c)}f=n(u),a=n(c)}var l=i(f,a,o);return(0===l||null==f&&null==a)&&e.length>t?r(e[t],e,t+1,o,i,u,c):l}};function o(r,n,e,t){return Array.isArray(n)?(Array.isArray(e)&&e.length<2&&(e=e[0]),n.sort(function r(e,t,o){if(void 0===e||!0===e)return function(r,n){return t(r,n,o)};if("string"==typeof e)return c(e.includes("."),"String syntax not allowed for nested properties."),function(r,n){return t(r[e],n[e],o)};if("function"==typeof e)return function(r,n){return t(e(r),e(n),o)};if(Array.isArray(e)){var i=f(t);return function(r,n){return i(e[0],e,1,o,t,r,n)}}var n=d(e);return r(n.sortBy,n.comparer||t,n.order)}(e,t,r))):n}function n(e){var t=u(e.comparer);return function(r){var n=Array.isArray(r)&&!e.inPlaceSorting?r.slice():r;return{asc:function(r){return o(1,n,r,t)},desc:function(r){return o(-1,n,r,t)},by:function(r){return o(1,n,r,t)}}}}function e(r,n,e){return null==r?e:null==n?-e:r<n?-1:r===n?0:1}var t=n({comparer:e}),i=n({comparer:e,inPlaceSorting:!0});r.createNewSortInstance=n,r.inPlaceSort=i,r.sort=t,Object.defineProperty(r,"__esModule",{value:!0})});
{
"name": "fast-sort",
"version": "2.2.0",
"version": "3.0.0",
"description": "Fast and powerful array sorting. Sort by any property in any direction with easy to read syntax.",
"main": "dist/sort.js",
"main": "dist/sort.min.js",
"module": "dist/sort.es.js",

@@ -40,18 +40,19 @@ "author": "Stefan Novakovic <stefan.novakovich@gmail.com>",

"devDependencies": {
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
"@typescript-eslint/eslint-plugin": "^2.31.0",
"@typescript-eslint/parser": "^2.31.0",
"chai": "^4.2.0",
"eslint": "^7.0.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-plugin-import": "^2.20.2",
"mocha": "^7.1.2",
"rollup": "^2.8.2",
"rollup-plugin-typescript2": "^0.27.0",
"@types/chai": "^4.2.15",
"@types/mocha": "^8.2.1",
"@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.17.0",
"chai": "^4.3.3",
"eslint": "^7.21.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"mocha": "^8.3.1",
"rollup": "^2.41.1",
"rollup-plugin-typescript2": "^0.30.0",
"rollup-plugin-uglify": "^6.0.4",
"ts-node": "^8.10.1",
"typescript": "^3.8.3",
"ts-node": "^9.1.1",
"tslib": "^2.1.0",
"typescript": "^4.2.3",
"watch": "^1.0.2"
}
}

@@ -18,7 +18,7 @@ # fast-sort

```javascript
import sort from 'fast-sort';
import { sort } from 'fast-sort';
// Sort flat arrays
sort([1,4,2]).asc(); // => [1, 2, 4]
sort([1, 4, 2]).desc(); // => [4, 2, 1]
const ascSorted = sort([1,4,2]).asc(); // => [1, 2, 4]
const descSorted = sort([1, 4, 2]).desc(); // => [4, 2, 1]

@@ -34,9 +34,18 @@ // Sort users (array of objects) by firstName in descending order

// Sort users ascending by firstName and descending by age
// Sort users ascending by firstName and descending by city
sort(users).by([
{ asc: u => u.firstName },
{ desc: u => u.age }
{ desc: u => u.address.city }
]);
// Sort based on computed property
sort(repositories).desc(r => r.openIssues + r.closedIssues);
// Sort using string for object key
// Only available for root object properties
sort(users).asc('firstName');
```
Fore even more examples check [unit tests](https://github.com/snovakovic/fast-sort/blob/master/test/sort.spec.ts).
## Highlights

@@ -53,78 +62,32 @@

* TypeScript support
* Small footprint with 0 dependencies (~ 750 bytes gzip)
* Packed with features in small footprint with 0 dependencies (~ 850 bytes gzip)
* Compatible with any JS environment as Node, Web, etc..
Under the hood sort is using [native JavaScript sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).
Usage of native sort implies that sorting is not necessarily [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability) and it also implies that input array is modified(sorted) same as it would be when applying native sort.
## Migrating from older versions
## More examples
Documentation for v2 and older versions is available [here](https://github.com/snovakovic/fast-sort/blob/v2/README.md).
* `asc` / `desc` sorters. Both asc and desc sorters have exactly the same API.
```javascript
import sort from 'fast-sort';
For migrating to v3 you can reference [CHANGELOG](https://github.com/snovakovic/fast-sort/blob/master/CHANGELOG.md) for what has been changed.
// Sort flat arrays
sort([1,4,2]).asc(); // => [1, 2, 4]
## In place sorting
// Sort array of objects by single object property
sort(users).asc(u => u.firstName);
By default `sort` does not mutate provided array it creates new "sorted" instance of array. `inPlaceSort` on other hand mutates provided array by sorting it without creating new array instance. Benefits of `inPlaceSort` is that it's slightly faster and more generous on memory as it's not creating new array instance every time sorting is done. Other than that there is no difference between using one or another.
// For root object properties we can use string shorthand (same as example above)
sort(users).asc('firstName');
// Sort by nested object properties
// NOTE: for nested object properties we can't use string shorthand ('address.city' is not valid syntax).
sort(users).asc(u => u.address.city);
// Sort by multiple properties
sort(users).asc([
u => u.age,
u => u.firstName,
]);
// Same as above but using string shorthand
sort(users).asc(['age', 'firstName']);
// Sort based on computed property
// For example sort repositories by total number of issues (summary of open and closed issues)
sort(repositories).desc(r => r.openIssues + r.closedIssues);
```
* `by` sorter can do anything that `asc` / `desc` sorters can with addition to some more advance
sort handling. With `by` sorter we can sort by multiple properties in different directions and
we can override default `comparer` for e.g natural sort purposes.
```javascript
import sort from 'fast-sort';
const { sort, inPlaceSort } = require('fast-sort');
// Sort users by firstName in ascending order and age in descending order
sort(users).by([
{ asc: u => u.firstName },
{ desc: u => u.age },
]);
const array = [3, 1, 5];
const sorted = sort(array).asc();
// Same as with asc/desc sorters we can use string shorthand for root object properties
sort(users).by([{ asc: 'firstName' }, { desc: 'age' }]);
// sorted => [1, 3, 5]
// array => [3, 1, 5]
// Sort users by city using custom comparer
sort(users).by({
asc: u => u.address.city,
comparer: (a, b) => a.localeCompare(b),
});
inPlaceSort(array).asc();
// Sort users ascending by age using default comparer and then by lastName using language sensitive comparer
sort(users).by([
{ asc: 'age' },
{
asc: 'lastName',
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare,
},
]);
// array => [1, 3, 5]
```
* Fore even more examples check unit tests `test/sort.spec.ts` in the github repo.
## Natural sorting / Language sensitive sorting
### Natural sorting / Language sensitive sorting
By default `fast-sort` is not doing language sensitive sorting of strings.

@@ -137,3 +100,3 @@ e.g `'image-11.jpg'` will be sorted before `'image-2.jpg'` (in ascending sorting).

```javascript
import sort from 'fast-sort';
import { sort, createNewSortInstance } from 'fast-sort';

@@ -145,3 +108,4 @@ const testArr = ['image-2.jpg', 'image-11.jpg', 'image-3.jpg'];

// We can use `by` sort to override default comparer with the one that is doing language sensitive comparison
// We can use `by` sort to override default comparer
// with the one that is doing language sensitive comparison
sort(testArr).by({

@@ -152,6 +116,5 @@ desc: true,

// Or we can create new sort instance with language sensitive comparer.
// Recommended if used in multiple places
const naturalSort = sort.createNewInstance({
const naturalSort = createNewSortInstance({
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare,

@@ -164,3 +127,3 @@ });

### Custom sorting
## Custom sorting

@@ -176,14 +139,19 @@ Fast sort can be tailored to fit any sorting need or use case by:

```javascript
import sort from 'fast-sort';
import { sort, createNewSortInstance } from 'fast-sort';
const tagsImportance = { vip: 3, influencer: 2, captain: 1 }; // Some domain specific logic
const tags = ['influencer', 'unknown', 'vip', 'captain'];
const tagsImportance = { // Domain specific tag importance
vip: 3,
influencer: 2,
captain: 1,
};
// Sort tags in ascending order by custom tags values
sort(tags).asc(tag => tagImportance[tag] || 0); // => ['unknown', 'captain', 'influencer', 'vip'];
sort(tags).desc(tag => tagImportance[tag] || 0); // => ['vip', 'influencer', 'captain', 'unknown'];
// We can use power of computed prop to sort tags by domain specific importance
const descTags = sort(tags).desc(tag => tagImportance[tag] || 0);
// => ['vip', 'influencer', 'captain', 'unknown'];
// We can also create specialized tagSorter instance and reuse it across the application
const tagSorter = sort.createNewInstance({
comparer: (a, b) => (tagImportance[a] || 0) - (tagImportance[b] || 0)
// Or we can create specialized tagSorter so we can reuse it in multiple places
const tagSorter = createNewSortInstance({
comparer: (a, b) => (tagImportance[a] || 0) - (tagImportance[b] || 0),
inPlaceSorting: true, // default[false] => Check "In Place Sort" section for more info.
});

@@ -194,10 +162,7 @@

// Default sorter will sort tags by string comparison and not "tag" importance
sort(tags).asc(); // => ['captain', 'influencer', 'unknown' 'vip']
// Default sorter will sort tags by comparing string values not by their domain specific value
const defaultSort = sort(tags).asc(); // => ['captain', 'influencer', 'unknown' 'vip']
```
## More examples
### Things to know
When using custom comparers as e.g [Intl.Collator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator) it's up to you to ensure those features are available in all the platforms you intend to support. (You can check browser compatibility for Intl.Collator by following link above). Same applies for any other custom comparer.
```javascript

@@ -208,75 +173,10 @@ // Sorting values that are not sortable will return same value back

// By default sort will mutate input array (by sorting it),
const arr = [1, 4, 2];
const sortedArr = sort(arr).asc();
console.log(sortedArr); // => [1, 2, 4]
console.log(arr); // => [1, 2, 4]
console.log(sortedArr === arr), // => true
// TIP: to prevent mutating of input array you can clone it before passing to sort as
const arr = [1, 4, 2];
const sortedArr = sort([...arr]).asc();
console.log(arr); // => [1, 4, 2]
console.log(sortedArr); // => [1, 2, 4]
console.log(sortedArr === arr), // => false
// As stated in highlights by default fast-sort sorts null and undefined values to the
// By default fast-sort sorts null and undefined values to the
// bottom no matter if sorting is in asc or decs order.
// If this is not intended behaviour you can check "Should create sort instance that sorts nil value to the top in desc order" test on how to override
const addresses = [{ city: 'Split' }, { city: undefined }, { city: 'Zagreb'}];
sort(addresses).asc(a => a.city); // => Split, Zagreb, undefined
sort(addresses).desc(a => a.city); // => Zagreb, Split, undefined
// If above is not intended behaviour you can always create new sort instance that will sort null
// or undefined values the way you intended it to be. For example of exactly that you can check unit test
// "Should create sort instance that sorts nil value to the top in desc order" in 'test/sort.spec.ts'
```
### Usage with ts-node
In a nodeJS environment, when fast-sort is being imported with ts-node, you might see an error along the lines of:
```
TypeError {
message: 'fast_sort_1.default is not a function',
}
```
In this case just add this to your `tsconfig.json`:
```json
{
"compilerOptions": {
"esModuleInterop": true
}
}
```
### Fast sort versions
#### `v2` version
There is no breaking changes in API between `v2` and `v1` version of library.
Some import files have been removed so if you haven't import it using default import
you might need to update imports. For more info check [v2 release notes](https://github.com/snovakovic/fast-sort/releases/tag/v2.0.0)
#### Features by version
```javascript
// Sorting in multiple directions is available from [v1.5.0]
sort(users).by([{ asc: 'age' }, { desc: 'firstName' }]);
// Overriding of default comparer in `by` sorter is available from [v1.6.0]
sort(testArr).by({
desc: true,
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare,
});
// Creating new custom sort instances is available from [v2.0.0]
const naturalSort = sort.createNewInstance({
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare,
});
// TypeScript support is available from [v2.0.0]
```
## Benchmark

@@ -283,0 +183,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc