
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
sort-predicate
Advanced tools
A simple utility for creating flexible sorting predicates for arrays of objects in JavaScript and TypeScript.
A simple utility for creating flexible sorting predicates for arrays of objects in JavaScript and TypeScript.
null
and undefined
values gracefullyInstall the package via npm:
npm install sort-predicate
The createSortPredicate function generates a sorting predicate that can be used to sort arrays of objects by a specified field.
import { createSortPredicate } from 'sort-predicate';
interface IModel {
name: string;
}
const byName = createSortPredicate<IModel>("name");
const byNameDesc = createSortPredicate<IModel>("name", "desc");
const arr = [{ name: "002" }, { name: "001" }, { name: "003" }];
console.log(arr.sort(byName)); // Output: [{ name: "001" }, { name: "002" }, { name: "003" }]
console.log(arr.sort(byNameDesc)); // Output: [{ name: "003" }, { name: "002" }, { name: "001" }]
createSortPredicate<T>(field: keyof T, order: 'asc' | 'desc' = 'asc')
Returns a predicate function that can be used with Array.sort().
This package includes tests written in Jest. To run the tests, simply execute:
npm test
This project is licensed under the MIT License.
FAQs
A simple utility for creating flexible sorting predicates for arrays of objects in JavaScript and TypeScript.
We found that sort-predicate 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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.