two-screen / stable Goto Github PK
View Code? Open in Web Editor NEWA stable array sort for JavaScript
A stable array sort for JavaScript
While this module has been relatively stable, certain consumers can be reluctant to use it because of the fact that it has not yet entered "stable" stage by semver. Would it be possible to publish this module as 1.0.0 on npm?
I'm using accept-language node module which includes stable like this:
var stable = require("stable");
My whole project is bundled and packaged with webpack 4.5, and since this new release it includes require like this:
var stable = webpack_require(/*! stable */ "./node_modules/stable/stable.esm.js");
but unfortanutenaly this leads to "stable is not a function" errors.I guess the problem is that it should actually be a "import stable from.." statemetn instead of the regular requrie to let this work correctly. Or rewrite the stable call to stable.default(args...). I guess there are more situations like this though, and removing the "module" line in package.json fixes everything.
Not quite sure what is the correct approach to handle this. All I know: it doesn't work right now, and stable is the only package in my repro right now that has this kind of issue?
In the case where the comparator returns NaN
(such as (A,B) => B-A
where either, or both, are undefined
) the behaviour of this lib differs from that of vanilla JS:
console.error([1, 2, 3].sort(() => NaN))
console.error(stable.inplace([1, 2, 3], () => NaN))
[ 1, 2, 3 ]
[ 3, 2, 1 ]
console.error([3, 2, 1].sort(() => NaN))
console.error(stable.inplace([3, 2, 1], () => NaN))
[ 3, 2, 1 ]
[ 1, 2, 3 ]
It's really the fault of the comparator function since it should only return a finite number, but in the case where it doesn't, I would argue that it's preferable to treat NaN
as 0
, so as to not affect the order.
It's unfortunately an easy mistake to make, such as when sorting numerically:
(A, B) => B - A
If either of the values is undefined
(or both!) it triggers this issue.
Per @kriskowal's suggestion in #1.
See if we can add support for in-place sorting.
A stable sort is nice because you can sort it over and over again by the same criteria to get nested sort orders.
My question, would it be faster to also enable multiple levels of comparator and run the sort once. The alternative would be to sort and resort with each comparator.
Option 1, multiple levels of comparators
stable.inplace(array, [byName, byValue, byId])
vs Options 2, multiple sorts
stable.inplace(array, byId);
stable.inplace(array, byValue);
stable.inplace(array, byName);
Also would be nice to sort by fields with writing the sort function:
stable.inplace(array, "id");
stable.inplace(array, "value");
stable.inplace(array, "name");
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.