-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathREADME.hbs
205 lines (156 loc) · 5.89 KB
/
README.hbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
[](https://www.npmjs.org/package/sort-array)
[](https://www.npmjs.org/package/sort-array)
[](https://github.com/75lb/sort-array/network/dependents?dependent_type=REPOSITORY)
[](https://github.com/75lb/sort-array/network/dependents?dependent_type=PACKAGE)
[](https://github.com/75lb/sort-array/actions/workflows/node.js.yml)
[](https://github.com/feross/standard)
# sort-array
Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order.
```js
const sortArray = require('sort-array')
```
## Synopsis
Some trivial examples to demonstrate typical usage.
### Sorting an array of primitives
#### Ascending order
Sort an array of strings in ascending order (the default).
```js
> const partsOfTheDay = ['twilight', 'afternoon', 'morning', 'evening']
> sortArray(partsOfTheDay)
[ 'afternoon', 'evening', 'morning', 'twilight' ]
```
#### Descending order
Sort an array of strings in descending order.
```js
> sortArray(partsOfTheDay, { order: 'desc' })
[ 'twilight', 'morning', 'evening', 'afternoon' ]
```
#### Custom sort order
The default value for `options.order` is `'asc'`. You can also specify `'desc'` or the name of a property from the `customOrders` object. For example, sort parts of the day by the order in which they occur.
```js
> sortArray(partsOfTheDay, {
order: 'time',
customOrders: {
time: ['morning', 'afternoon', 'evening', 'twilight']
}
})
[ 'morning', 'afternoon', 'evening', 'twilight' ]
```
### Sorting an array of objects
#### Sort by object property
Pass one or more property names to `options.by` to sort an array of objects by those properties.
```js
> const repositories = [
{ name: '75lb/sort-array', openIssues: 0, closedIssues: 4 },
{ name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 },
{ name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 }
]
> sortArray(repositories, {
by: 'openIssues',
order: 'desc'
})
[
{ name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 },
{ name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 },
{ name: '75lb/sort-array', openIssues: 0, closedIssues: 4 }
]
```
#### Sort by computed field
Sort by a computed field, i.e. a computed value that doesn't exist in the input dataset. Define your computed fields in the `options.computed` object, each value being a function which takes an array member as input and returns the primitive value to be sorted by. In this example we sort by `total` (the name of the computed field supplied in `options.computed`).
```js
> const repositories = [
{ name: '75lb/sort-array', openIssues: 0, closedIssues: 4 },
{ name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 },
{ name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 }
]
> sortArray(repositories, {
by: 'total',
order: 'desc',
computed: {
total: repository => repository.openIssues + repository.closedIssues
}
})
[
{ name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 },
{ name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 },
{ name: '75lb/sort-array', openIssues: 0, closedIssues: 4 }
]
```
#### Sort by deep object values
You can use computed fields to sort by values deep in an object structure.
```js
> const data = [
{ inner: { number: 2 } },
{ inner: { number: 3 } },
{ inner: { number: 1 } }
]
> sortArray(data, {
by: 'number',
computed: {
number: row => row.inner.number
}
})
[
{ inner: { number: 1 } },
{ inner: { number: 2 } },
{ inner: { number: 3 } }
]
```
#### Sort by multiple fields
Sort by multiple columns using multiple custom orders.
```js
> const attributes = [
{ skill: 'accuracy', confidence: 'medium' },
{ skill: 'power', confidence: 'high' },
{ skill: 'speed', confidence: 'low' },
{ skill: 'power', confidence: 'low' },
{ skill: 'speed', confidence: 'high' },
{ skill: 'accuracy', confidence: 'low' },
{ skill: 'speed', confidence: 'medium' },
{ skill: 'accuracy', confidence: 'high' },
{ skill: 'power', confidence: 'medium' }
]
> sortArray(attributes, {
by: ['skill', 'confidence'],
order: ['skill', 'confidence'],
customOrders: {
skill: ['accuracy', 'speed', 'power'],
confidence: ['low', 'medium', 'high'],
}
})
[
{ skill: 'accuracy', confidence: 'low' },
{ skill: 'accuracy', confidence: 'medium' },
{ skill: 'accuracy', confidence: 'high' },
{ skill: 'speed', confidence: 'low' },
{ skill: 'speed', confidence: 'medium' },
{ skill: 'speed', confidence: 'high' },
{ skill: 'power', confidence: 'low' },
{ skill: 'power', confidence: 'medium' },
{ skill: 'power', confidence: 'high' }
]
```
Please visit [the sort-array wiki](https://github.com/75lb/sort-array/wiki) for more examples.
## API Reference
{{>main}}
## Load anywhere
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
Node.js:
```js
const sortArray = require('sort-array')
```
Within Node.js with ECMAScript Module support enabled:
```js
import sortArray from 'sort-array'
```
Within an modern browser ECMAScript Module:
```js
import sortArray from './node_modules/sort-array/dist/index.mjs'
```
Old browser (adds `window.sortArray`):
```html
<script nomodule src="./node_modules/sort-array/dist/index.js"></script>
```
* * *
© 2015-24 Lloyd Brookes \<75pound@gmail.com\>.
Tested by [test-runner](https://github.com/test-runner-js/test-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).