Installations
npm install @leondreamed/fast-sort
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
18.12.1
NPM Version
8.19.2
Score
71.3
Supply Chain
99.3
Quality
75.2
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (69.57%)
JavaScript (30.43%)
Developer
snovakovic
Download Statistics
Total Downloads
4,004
Last Day
2
Last Week
3
Last Month
10
Last Year
479
GitHub Statistics
323 Stars
320 Commits
28 Forks
4 Watching
2 Branches
4 Contributors
Bundle Size
1.75 kB
Minified
819.00 B
Minified + Gzipped
Package Meta Information
Latest Version
3.2.0
Package Id
@leondreamed/fast-sort@3.2.0
Unpacked Size
26.64 kB
Size
7.44 kB
File Count
8
NPM Version
8.19.2
Node Version
18.12.1
Total Downloads
Cumulative downloads
Total Downloads
4,004
Last day
0%
2
Compared to previous day
Last week
0%
3
Compared to previous week
Last month
0%
10
Compared to previous month
Last year
-85.4%
479
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
fast-sort
Fast easy to use and flexible sorting with TypeScript support.
For speed comparison of fast-sort
vs other popular sort libraries check benchmark section.
For list of all available features check highlights section.
Quick examples
1 import { sort } from 'fast-sort'; 2 3 // Sort flat arrays 4 const ascSorted = sort([1,4,2]).asc(); // => [1, 2, 4] 5 const descSorted = sort([1, 4, 2]).desc(); // => [4, 2, 1] 6 7 // Sort users (array of objects) by firstName in descending order 8 const sorted = sort(users).desc(u => u.firstName); 9 10 // Sort users in ascending order by firstName and lastName 11 const sorted = sort(users).asc([ 12 u => u.firstName, 13 u => u.lastName 14 ]); 15 16 // Sort users ascending by firstName and descending by city 17 const sorted = sort(users).by([ 18 { asc: u => u.firstName }, 19 { desc: u => u.address.city } 20 ]); 21 22 // Sort based on computed property 23 const sorted = sort(repositories).desc(r => r.openIssues + r.closedIssues); 24 25 // Sort using string for object key 26 // Only available for root object properties 27 const sorted = sort(users).asc('firstName');
Fore more examples check unit tests.
Highlights
- Sort flat arrays
- Sort array of objects by one or more properties
- Sort in multiple directions
- Natural sort support
- Support for custom sort instances
- Easy to read syntax
- Faster than other popular sort alternatives
- Undefined and null values are always sorted to bottom (with default comparer)
- TypeScript support
- Packed with features in small footprint with 0 dependencies (~ 850 bytes gzip)
- Compatible with any JS environment as Node, Web, etc..
Migrating from older versions
Documentation for v2 and older versions is available here.
For migrating to v3 you can reference CHANGELOG for what has been changed.
In place sorting
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.
1const { sort, inPlaceSort } = require('fast-sort'); 2 3const array = [3, 1, 5]; 4const sorted = sort(array).asc(); 5 6// sorted => [1, 3, 5] 7// array => [3, 1, 5] 8 9inPlaceSort(array).asc(); 10 11// array => [1, 3, 5]
Natural sorting / Language sensitive sorting
By default fast-sort
is not doing language sensitive sorting of strings.
e.g 'image-11.jpg'
will be sorted before 'image-2.jpg'
(in ascending sorting).
We can provide custom Intl.Collator comparer to fast-sort for language sensitive sorting of strings.
Keep in mind that natural sort is slower then default sorting so recommendation is to use it
only when needed.
1 import { sort, createNewSortInstance } from 'fast-sort'; 2 3 const testArr = ['image-2.jpg', 'image-11.jpg', 'image-3.jpg']; 4 5 // By default fast-sort is not doing natural sort 6 sort(testArr).desc(); // => ['image-3.jpg', 'image-2.jpg', 'image-11.jpg'] 7 8 // We can use `by` sort to override default comparer 9 // with the one that is doing language sensitive comparison 10 sort(testArr).by({ 11 desc: true, 12 comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare, 13 }); // => ['image-11.jpg', 'image-3.jpg', 'image-2.jpg'] 14 15 // Or we can create new sort instance with language sensitive comparer. 16 // Recommended if used in multiple places 17 const naturalSort = createNewSortInstance({ 18 comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare, 19 }); 20 21 naturalSort(testArr).asc(); // => ['image-2.jpg', 'image-3.jpg', 'image-11.jpg'] 22 naturalSort(testArr).desc(); // => ['image-11.jpg', 'image-3.jpg', 'image-2.jpg']
NOTE: It's known that Intl.Collator
might not sort null
values correctly so make sure to cast them to undefine
as described in the following issue
https://github.com/snovakovic/fast-sort/issues/54#issuecomment-1072289388
Custom sorting
Fast sort can be tailored to fit any sorting need or use case by:
- creating custom sorting instances
- overriding default comparer in
by
sorter - custom handling in provided callback function
- combination of any from above
For example we will sort tags
by "custom" tag importance (e.g vip
tag is of greater importance then captain
tag).
1 import { sort, createNewSortInstance } from 'fast-sort'; 2 3 const tags = ['influencer', 'unknown', 'vip', 'captain']; 4 const tagsImportance = { // Domain specific tag importance 5 vip: 3, 6 influencer: 2, 7 captain: 1, 8 }; 9 10 // We can use power of computed prop to sort tags by domain specific importance 11 const descTags = sort(tags).desc(tag => tagImportance[tag] || 0); 12 // => ['vip', 'influencer', 'captain', 'unknown']; 13 14 // Or we can create specialized tagSorter so we can reuse it in multiple places 15 const tagSorter = createNewSortInstance({ 16 comparer: (a, b) => (tagImportance[a] || 0) - (tagImportance[b] || 0), 17 inPlaceSorting: true, // default[false] => Check "In Place Sort" section for more info. 18 }); 19 20 tagSorter(tags).asc(); // => ['unknown', 'captain', 'influencer', 'vip']; 21 tagSorter(tags).desc(); // => ['vip', 'influencer', 'captain', 'unknown']; 22 23 // Default sorter will sort tags by comparing string values not by their domain specific value 24 const defaultSort = sort(tags).asc(); // => ['captain', 'influencer', 'unknown' 'vip']
More examples
1 // Sorting values that are not sortable will return same value back 2 sort(null).asc(); // => null 3 sort(33).desc(); // => 33 4 5 // By default fast-sort sorts null and undefined values to the 6 // bottom no matter if sorting is in asc or decs order. 7 // 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 8 const addresses = [{ city: 'Split' }, { city: undefined }, { city: 'Zagreb'}]; 9 sort(addresses).asc(a => a.city); // => Split, Zagreb, undefined 10 sort(addresses).desc(a => a.city); // => Zagreb, Split, undefined
Benchmark
Five different benchmarks have been created to get better insight of how fast-sort perform under different scenarios. Each benchmark is run with different array sizes raging from small 100 items to large 100 000 items.
Every run of benchmark outputs different results but the results are constantly showing better scores compared to similar popular sorting libraries.
Benchmark scores
Benchmark has been run on:
- 16 GB Ram
- Intel® Core™ i5-4570 CPU @ 3.20GHz × 4
- Ubuntu 16.04
- Node 8.9.1
Independent benchmark results from MacBook Air can be found in following PR: https://github.com/snovakovic/fast-sort/pull/48
Running benchmark
To run benchmark on your PC follow steps from below
- git clone https://github.com/snovakovic/fast-sort.git
- cd fast-sort/benchmark
- npm install
- npm start
In case you notice any irregularities in benchmark or you want to add sort library to benchmark score please open issue here
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/16 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 18 are checked with a SAST tool
Reason
16 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-7wpw-2hjm-89gp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
Score
1.7
/10
Last Scanned on 2025-01-20
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More