
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
natural-order
Advanced tools
Sorting with support for numbers, dates, unicode and more.
// ES6
import naturalOrder from "natural-order";
// CommonJS
const naturalOrder = require("natural-order");
naturalOrder: <A>(list: A[]) => NaturalList<A>
class NaturalList<A> {
with: (options: { blankAtTop?: boolean, caseSensitive?: boolean}) => NaturalList<A>
orderBy: (order: Array<"desc" | "asc"> | Array<1 | -1> | 1 | -1 | "desc" | "asc") => NaturalList<A>
sort: (sortBy?: string[]) => A[]
}
list: A[]
Any list (strings, numbers, or objects)
options: { blankAtTop?: boolean, caseSensitive?: boolean}
Optional parameters:
order: 1 | -1 | "asc" | "desc" | ("asc" | "desc")[] | (1 | -1)[]
Order by which to sort. Defaults to ascending. Enter a value for each key you are using for sorting. If not enough values are passed, the last provided will be used when they run out. (example: You may just pass "desc", and all keys will be sorted in descending order.)
The number values 1 and -1 can be used instead of "asc" and "desc", respectively.
sortBy?: string[]
The keys by which to sort. May be null. If sorting objects, defaults to the first key it finds.
const list = ["b", "z", "a"];
naturalOrder(list).sort();
// ["a", "b", "z"]
naturalOrder(list).orderBy("desc").sort();
// ["z", "b", "a"]
naturalOrder(list).orderBy(-1).sort();
// ["z", "b", "a"]
const list2 = [{ name: "George" }, { name: "Fred" }, { name: "Alice" }];
naturalOrder(list2).sort(["name"]);
// [{name: "Alice"}, {name: "Fred""}, {name: "George"}]
const list3 = [
{ name: { first: "bob", last: "temple" } },
{ name: { first: "steve", last: "martin" } },
{ name: { first: "george", last: "martin" } },
{ name: { first: "adam", last: "temple" } }
];
naturalOrder(list3).sort(["name.last", "name.first"]);
// [ { name: { first: 'george', last: 'martin' } },
// { name: { first: 'steve', last: 'martin' } },
// { name: { first: 'adam', last: 'temple' } },
// { name: { first: 'bob', last: 'temple' } } ]
naturalOrder(list3).sort();
// [ { name: { first: 'adam', last: 'temple' } },
// { name: { first: 'bob', last: 'temple' } },
// { name: { first: 'george', last: 'martin' } },
// { name: { first: 'steve', last: 'martin' } } ]
const list4 = ["a", "B"];
naturalOrder(list4).with({ caseSensitive: true }).sort();
// ["B", "a"]
const list5 = ["z", "", "a"];
naturalOrder(list5).sort();
// ["a", "z", ""]
naturalOrder(list5).with({ blankAtTop: true }).sort();
// ["", "a", "z"]
There are two major changes in version 2. First, the default import is now using ESM instead of CommonJS. If you are using natural-order in an environment that requires CommonJS, you will need to directly import natural-order/dist/natural-order.umd.js.
Second, due to the focus being immutable sorting of values, I've removed the naturalSort method. While it was convenient to plug into the standard array.sort function, it did not maintain the immutability the main function provides. You will need to migrate to using naturalOrder directly.
All options from verion 0.3.0 are still available with the new API. Alternatively, if you prefer the old syntax, it is still available, but you will need to call .sort() still.
const list = ["a", "b", "c", "A"]
// Old syntax
const sorted1 = naturalOrder(list, null, "desc", { caseSensitive: true })
// New syntax
const sorted2 = naturalOrder(list).with({ caseSensitive: true }).orderBy("desc").sort()
sorted1[0] === sorted2[0] // true
// Alternative syntax
const sorted3 = naturalOrder(list, null, "desc", { caseSensitive: true }).sort()
sorted1[0] === sorted3[0] // true
This project uses code from natural-sort for its natural sorting method.
This project is MIT Licensed.
FAQs
Sort an array of strings, numbers, or objects naturally.
The npm package natural-order receives a total of 72 weekly downloads. As such, natural-order popularity was classified as not popular.
We found that natural-order demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.