Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
better-sort
Advanced tools
Syntactic sugar wrapper over JS sort function with better readability
Zero-dependency minimal package (two functions) to provide better-readable (albeit more verbose) wrapper over JS default Array.sort()
. Fully typed.
Prevents errors when interchanging signs in the native Array.sort()
function like this:
foo.sort((a, b) => (a.date.isBefore(b.date) ? -1 : 1))
- at the first glance, is this sorted in ASC or DESC order??
Usage:
import { sortAsc } from "./src/index";
const array = [3, 1, 4, 1, 5, 9, 2, 6];
const sortedArray = sortAsc({
array,
firstIsGreater: (a, b) => a > b, // Compare function that returns boolean
});
console.log(sortedArray); // [1, 1, 2, 3, 4, 5, 6, 9]
console.log(array); // [3, 1, 4, 1, 5, 9, 2, 6] - keeps the original array intact
You can provide areEqual
function parameter to ensure stability of the sorting function. Otherwise it sorts equal elements in non-deterministic order.
import { sortAsc } from "./src/index";
// The sortAsc function is a stable sort when `areEqual` is provided, meaning that it preserves the original input order of elements that are equal.
const inputArray = [
{ name: "John", age: 25 },
{ name: "Alice", age: 30 },
{ name: "Bob", age: 20 },
{ name: "Mike", age: 25 },
];
const sortedArray = sortAsc({
array: inputArray,
firstIsGreater: (a, b) => a > b,
areEqual: (a, b) => a === b,
});
console.log(sortedArray); // [{ name: "Bob", age: 20 }, { name: "John", age: 25 }, { name: "Mike", age: 25 }, { name: "Alice", age: 30 }] - keeps equal elements in order they appear in input
FAQs
Syntactic sugar wrapper over JS sort function with better readability
We found that better-sort demonstrated a healthy version release cadence and project activity because the last version was released less than 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.