Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/damianc/sortby
Sorts arrays of objects by multiple fields.
https://github.com/damianc/sortby
Last synced: about 1 month ago
JSON representation
Sorts arrays of objects by multiple fields.
- Host: GitHub
- URL: https://github.com/damianc/sortby
- Owner: damianc
- Created: 2023-12-13T02:26:22.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-12-13T02:29:05.000Z (about 1 year ago)
- Last Synced: 2023-12-13T03:58:09.506Z (about 1 year ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `sortBy()`
```
sortBy(fields: string[]): void
```Sorts an array with objects by given fields.
- to sort in ascending order: pass a field name (optionally, it can start with plus sign `+`)
- to sort in descending order: pass a field name that starts with minus sign `-`## Example
```
const data = [{
id: 2, name: 'Adam', city: 'NY'
}, {
id: 3, name: 'Mark', city: 'LA'
}, {
id: 1, name: 'John', city: 'NY'
}];data.sortBy(['city', '-id']);
console.log(
data
);/*
[
{
"id": 3,
"name": "Mark",
"city": "LA"
},
{
"id": 2,
"name": "Adam",
"city": "NY"
},
{
"id": 1,
"name": "John",
"city": "NY"
}
]
*/
```