Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Fast and powerful array sorting. Sort by any property in any direction with easy to read syntax.
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.
import sort from 'fast-sort';
// Sort flat arrays
sort([1,4,2]).asc(); // => [1, 2, 4]
sort([1, 4, 2]).desc(); // => [4, 2, 1]
// Sort users (array of objects) by firstName in descending order
sort(users).desc(u => u.firstName);
// Sort users in ascending order by firstName and lastName
sort(users).asc([
u => u.firstName,
u => u.lastName
]);
// Sort users ascending by firstName and descending by age
sort(users).by([
{ asc: u => u.firstName },
{ desc: u => u.age }
]);
Under the hood sort is using native JavaScript sort. Usage of native sort implies that sorting is not necessarily stable and it also implies that input array is modified(sorted) same as it would be when applying native sort.
asc
/ desc
sorters. In below examples we will use asc
sorter but keep in mind that both asc
and desc
sorters have exactly the same API so all the examples below can be applied for desc
sorter. import sort from 'fast-sort';
// Sort flat arrays
sort([1,4,2]).asc(); // => [1, 2, 4]
// Sort array of objects by single object property
sort(users).asc(u => u.firstName);
// 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. import sort from '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 },
]);
// Same as with asc/desc sorters we can use string shorthand for root object properties
sort(users).by([{ asc: 'firstName' }, { desc: 'age' }]);
// Sort users by city using custom comparer
sort(users).by([
asc: u => u.address.city,
comparer: (a, b) => a.localeCompare(b),
]);
// 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,
},
]);
test/sort.spec.ts
in the github repo.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.
import sort from 'fast-sort';
const testArr = ['image-2.jpg', 'image-11.jpg', 'image-3.jpg'];
// By default fast-sort is not doing natural sort
sort(testArr).desc(); // => ['image-3.jpg', 'image-2.jpg', 'image-11.jpg']
// We can use `by` sort to override default comparer with the one that are doing language sensitive comparison
sort(testArr).by({
desc: true,
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare,
}); // => ['image-11.jpg', 'image-3.jpg', 'image-2.jpg']
// If we want to reuse natural sort in multiple places we can create new sort instance
const naturalSort = sort.createNewInstance({
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare,
});
naturalSort(testArr).asc(); // => ['image-2.jpg', 'image-3.jpg', 'image-11.jpg']
naturalSort(testArr).desc(); // => ['image-11.jpg', 'image-3.jpg', 'image-2.jpg']
Fast sort can be tailored to fit any sorting need or use case by:
by
sorterFor example we will sort tags
by "custom" tag importance (e.g vip
tag is of greater importance then captain
tag).
import sort from 'fast-sort';
const tagsImportance = { vip: 3, influencer: 2, captain: 1 }; // Some domain specific logic
const tags = ['influencer', 'unknown', 'vip', 'captain'];
// 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 also create specialized tagSorter instance and reuse it across the application
const tagSorter = sort.createNewInstance({
comparer: (a, b) => (tagImportance[a] || 0) - (tagImportance[b] || 0)
});
tagSorter(tags).asc(); // => ['unknown', 'captain', 'influencer', 'vip'];
tagSorter(tags).desc(); // => ['vip', 'influencer', 'captain', 'unknown'];
// Default sorter will sort tags by string comparison and not "tag" importance
sort(tags).asc(); // => ['captain', 'influencer', 'unknown' 'vip']
When using custom comparers as e.g Intl.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.
// Sorting values that are not sortable will return same value back
sort(null).asc(); // => null
sort(33).desc(); // => 33
// 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]
// To prevent that we can use ES6 destructor (or ES5 equivalents)
const arr = [1, 4, 2];
const sortedArr = sort([...arr]).asc();
console.log(arr); // => [1, 4, 2]
console.log(sortedArr); // => [1, 2, 4]
// As stated in highlights fast-sort sorts null and undefined values to the
// bottom no matter if sorting is in asc or decs order.
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'
v2
versionThere is no breaking changes in API between v2
and v1
version of library.
You should be able to upgrade and take advantage of better editor support
and more flexibility without any problem.
Only consideration to take in mind is that some dist
import file names have been changed. Based on how you used library there is small chance you might need to update those imports as:
require('fast-sort/sort.es5')
should be updated to: require('fast-sort')
(es5 is now default export)require('fast-sort/sort.es5.min')
should be updated to: require('fast-sort/sort.min')
// 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]
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 has been run on:
To run benchmark on your PC follow steps from below
In case you notice any irregularities in benchmark or you want to add sort library to benchmark score please open issue here
[2.0.0] - 2019-12-14
const naturalSort = sort.createNewInstance({
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare,
});
FAQs
Fast easy to use and flexible sorting with TypeScript support
The npm package fast-sort receives a total of 82,649 weekly downloads. As such, fast-sort popularity was classified as popular.
We found that fast-sort demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.