2
2
3
3
<!-- markdownlint-disable MD033 MD034 MD014 -->
4
4
5
- The motivation for creating this utility was to provide a mechanism to organize elements,
6
- with the ability to specify multiple ordering criteria .
5
+ Sorts an array and allows specify multiple sorting criteria.
6
+ It has support for accented characters, and also ignore case sensitive .
7
7
8
8
## Content
9
9
10
10
1 . [ Getting started] ( #getting-started )
11
11
1 . [ Including the library] ( #including-the-library )
12
12
1 . [ Examples] ( #examples )
13
- 1 . [ Shimming-polyfills ] ( #shimming- polyfills )
13
+ 1 . [ Polyfills ] ( #polyfills )
14
14
1 . [ Running the project] ( #running-the-project )
15
15
16
16
## Getting started
@@ -25,41 +25,54 @@ $ npm install array-sort-by --save
25
25
$ yarn add array-sort-by
26
26
```
27
27
28
- The library has been written as an ** ES2015 Module ** and the exported module has the following signature:
28
+ The ` sortBy ` function has the following signature:
29
29
30
30
``` javascript
31
- /*
32
- * @param {Array} array: the list of elements to sort
33
- * @param {Function} parser: transforms each item and specifies the sort order
31
+ /**
32
+ * @param {Array} array : the collection of elements to sort
33
+ * @param {Function} parser : transforms each item and specifies the sorting order
34
34
* @return {Array}
35
35
*/
36
- function sortBy (array , /* optional*/ parser );
36
+ sortBy (array: Array , parser: Function ) : Array
37
+ sortBy (array: Array ) : Array
37
38
```
38
39
39
- The optional parameter `parser` is a function that transforms each element being iterated and sets the sort rules:
40
- ascending, descending, and multiple fields for sorting. It has the following signature:
40
+ The optional parameter ` parser ` is a function that transforms each element
41
+ being iterated and sets the sorting rules: _ ascending_ or _ descending_ , and
42
+ the option to specify multiple fields for sorting.
43
+
44
+ The ` parser ` callback has the following signature:
41
45
42
46
``` javascript
43
- /*
47
+ /**
44
48
* @param {Any} item : the element being iterated over the list
45
49
* @param {Number} index : the index of the element in the list
46
50
* @return {Any}
47
51
*/
48
- function parser(item, index);
52
+ parser (item: Any, index: Number ) : Any
53
+ parser (item: Any) : Any
49
54
```
50
55
56
+ [ ☗ ; Back to Index] ( #content )
57
+
51
58
## Including the library
52
59
53
- `array-sort-by` can be included directly from a CDN in your page :
60
+ ` array-sort-by ` can be included directly from a CDN in your site :
54
61
55
62
``` html
56
- <script src="https:// cdn.rawgit.com/jherax/array-sort-by/1.0.3/dist/sort-by.min.js"></script>
63
+ <!-- from unpkg.com -->
64
+ <script src =" https://unpkg.com/array-sort-by/dist/sort-by.min.js" ></script >
65
+
66
+ <!-- or from rawgit.com -->
67
+ <script src =" https://cdn.rawgit.com/jherax/array-sort-by/1.1.0/dist/sort-by.min.js" ></script >
57
68
```
58
69
59
- In the above case, the function [`sortBy`](#examples) is included as a global object in the browser.
70
+ In the above case, the function [ ` sortBy ` ] ( #examples ) is included as
71
+ global object in the browser.
60
72
61
- As this library is built as an [UMD](http:// davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/)
62
- (Universal Module Definition), it can be included from a module loader as AMD, CommonJS, or ES2015 Export.
73
+ As ` sortBy ` is built as [ UMD] _ (Universal Module Definition)_ , it can
74
+ be included from module loaders such as [ CommonJS] , [ ES2015 Export]
75
+ or [ AMD RequireJS] .
63
76
64
77
### CommonJS
65
78
@@ -88,7 +101,9 @@ require(['array-sort-by'], function(sortBy) {
88
101
});
89
102
```
90
103
91
- See an example with RequireJS here: http://jsfiddle.net/FdKTn/69/
104
+ See an example with RequireJS here: http://jsfiddle.net/FdKTn/73/
105
+
106
+ [ ☗ ; Back to Index] ( #content )
92
107
93
108
## Examples
94
109
@@ -98,7 +113,7 @@ See an example with RequireJS here: http://jsfiddle.net/FdKTn/69/
98
113
let arr = [10 , 8 , 5 , 3 , 0 , 7 , 4 , 5 , 1 ];
99
114
sortBy (arr);
100
115
101
- /*
116
+ /**
102
117
* expected:
103
118
* [0, 1, 3, 4, 5, 5, 7, 8, 10]
104
119
*/
@@ -110,7 +125,7 @@ sortBy(arr);
110
125
let arr = [5 , 1 , 8 , 0 , 3 , 7 , 10 , 4 , 3 , 8 ];
111
126
sortBy (arr, n => - n);
112
127
113
- /*
128
+ /**
114
129
* expected:
115
130
* [10, 8, 8, 7, 5, 4, 3, 3, 1, 0]
116
131
*/
@@ -122,7 +137,7 @@ sortBy(arr, n => -n);
122
137
let arr = [" 1983/03/06" , " 1980/12/24" , " 1985/08/31" , " 1983/03/05" ];
123
138
sortBy (arr, (s ) => - new Date (s));
124
139
125
- /*
140
+ /**
126
141
* expected:
127
142
* ["1985/08/31", "1983/03/06", "1983/03/05", "1980/12/24"]
128
143
*/
@@ -136,12 +151,50 @@ Because we use the minus **(-)** symbol to specify a descending order, it will p
136
151
let arr = [" 1983/03/06" , " 1980/12/24" , " 1985/08/31" , " 1983/03/05" ];
137
152
sortBy (arr, (s ) => " desc:" + s);
138
153
139
- /*
154
+ /**
140
155
* expected:
141
156
* ["1985/08/31", "1983/03/06", "1983/03/05", "1980/12/24"]
142
157
*/
143
158
```
144
159
160
+ ### Sorting accented words
161
+
162
+ ``` javascript
163
+ var arr = [' único' , ' cosas' , ' Árbol' , ' fútbol' , ' algo' ];
164
+ sortBy (arr);
165
+ /**
166
+ * expected:
167
+ * ["algo", "Árbol", "cosas", "fútbol", "único"]
168
+ */
169
+
170
+ sortBy (arr, item => ' desc:' + item);
171
+ /**
172
+ * expected:
173
+ * ["único", "fútbol", "cosas", "Árbol", "algo"]
174
+ */
175
+ ```
176
+
177
+ ### Sorting accented words by @n
178
+
179
+ ``` javascript
180
+ var arr = [
181
+ { n: ' Woche' },
182
+ { n: ' wöchentlich' },
183
+ { n: ' wäre' }
184
+ ];
185
+
186
+ sortBy (arr, item => item .n );
187
+
188
+ /**
189
+ * expected:
190
+ * [
191
+ * { n: "wäre" },
192
+ * { n: "Woche" },
193
+ * { n: "wöchentlich" }
194
+ * ]
195
+ */
196
+ ```
197
+
145
198
### Sorting DESC by @a , after ASC by @d (as Date)
146
199
147
200
``` javascript
@@ -154,7 +207,7 @@ let arr = [
154
207
155
208
sortBy (arr, (o ) => [- o .a , new Date (o .d )]);
156
209
157
- /*
210
+ /**
158
211
* expected:
159
212
* [
160
213
* { a: 8, d: "1983/03/06" },
@@ -165,53 +218,55 @@ sortBy(arr, (o) => [-o.a, new Date(o.d)]);
165
218
*/
166
219
```
167
220
168
- ### Sorting DESC by @name (ignore case sensitive)
221
+ ### Sorting DESC by @name
169
222
170
223
``` javascript
171
224
let arr = [
172
225
{ id: 4 , name: " Pedro" },
173
- { id: 6 , name: " Lucia " },
226
+ { id: 6 , name: " Lucía " },
174
227
{ id: 7 , name: " paco" },
175
228
{ id: 3 , name: " luis" }
176
229
];
177
230
178
- sortBy (arr, ( o ) => " DESC:" + o .name . toUpperCase () );
231
+ sortBy (arr, item => " DESC:" + item .name );
179
232
180
- /*
233
+ /**
181
234
* expected:
182
235
* [
183
236
* { id: 4, name: "Pedro" },
184
237
* { id: 7, name: "paco" },
185
238
* { id: 3, name: "luis" },
186
- * { id: 6, name: "Lucia " }
239
+ * { id: 6, name: "Lucía " }
187
240
* ]
188
241
*/
189
242
```
190
243
191
- ### Sorting ASC by @name (ignore case sensitive) , after DESC by @age , after ASC by @a
244
+ ### Sorting ASC by @name , after DESC by @age , after ASC by @a
192
245
193
246
``` javascript
194
247
let arr = [
195
248
{ a: 4 , age: 26 , name: " pedro" },
196
249
{ a: 6 , age: 32 , name: " Pedro" },
197
- { a: 7 , age: 26 , name: " Luis " },
198
- { a: 2 , age: 26 , name: " luis " }
250
+ { a: 7 , age: 26 , name: " Maria " },
251
+ { a: 2 , age: 26 , name: " maría " }
199
252
];
200
253
201
- sortBy (arr, ( o ) => [o .name . toUpperCase () , - o .age , o .a ]);
254
+ sortBy (arr, item => [item .name , - item .age , item .a ]);
202
255
203
- /*
256
+ /**
204
257
* expected:
205
258
* [
206
- * { a: 2, age: 26, name: "luis " },
207
- * { a: 7, age: 26, name: "Luis " },
259
+ * { a: 2, age: 26, name: "maría " },
260
+ * { a: 7, age: 26, name: "Maria " },
208
261
* { a: 6, age: 32, name: "Pedro" },
209
262
* { a: 4, age: 26, name: "pedro" }
210
263
* ]
211
264
*/
212
265
```
213
266
214
- ## Shimming-polyfills
267
+ [ ☗ ; Back to Index] ( #content )
268
+
269
+ ## Polyfills
215
270
216
271
This library is written using some of the new ES5/ES6 features. If you have
217
272
to support Non-standard-compliant browsers like Internet Explorer, you can
@@ -247,28 +302,29 @@ to the url, for example:
247
302
Read the list of available features:
248
303
[ Features and Browsers Supported] ( https://polyfill.io/v2/docs/features/ ) .
249
304
305
+ [ ☗ ; Back to Index] ( #content )
306
+
250
307
## Running the project
251
308
252
309
If you want to fork or build your own, you must run this project.
253
310
254
311
### Requirements
255
312
256
- 1 . Git ( [ git- linux] ( https://git-scm.com/book/en/v2/Getting-Started-Installing-Git )
257
- or [ git- windows] ( https://git-for-windows.github.io/ ) ).
313
+ 1 . Git on [ linux] ( https://git-scm.com/book/en/v2/Getting-Started-Installing-Git )
314
+ or [ windows] ( https://git-for-windows.github.io/ ) .
258
315
1 . [ Node.js] ( https://nodejs.org/en/ ) (latest stable version v6+).<br >
259
- It is preferable install ** [ nvm] ( https://github.com/creationix/nvm ) **
260
- (Node Version Manager ).
261
- 1 . [ Yarn] ( https://yarnpkg.com/en/docs/cli/ ) installed as a global package.
316
+ It is preferable install [ nvm] ( https://github.com/creationix/nvm )
317
+ (node version manager ).
318
+ 1 . [ Yarn] ( https://yarnpkg.com/en/docs/cli/ ) installed as global package.
262
319
263
- ** NOTE** : Consider install Node Version Manager (** nvm** ) to upgrade easily the NodeJS version.
264
- <br >Go to https://github.com/creationix/nvm and check the installation process for your OS.
320
+ ** NOTE** : Consider install Node Version Manager (** nvm** ) to upgrade easily
321
+ the Node version.<br >Go to https://github.com/creationix/nvm and check the
322
+ installation process for your OS.
265
323
266
- If you are running Windows, you can install
267
- [ nvm-windows] ( https://github.com/coreybutler/nvm-windows#node-version-manager-nvm-for-windows ) .
268
- Follow every step mentioned
269
- [ here] ( https://github.com/coreybutler/nvm-windows#installation--upgrades )
270
- so that nvm will be correctly installed to manage multiple installations
271
- of ** node.js** (with ** npm** ) on a Windows computer.
324
+ If you are running Windows, you can install [ nvm-windows] . Follow every
325
+ step mentioned [ here] [ nvm-windows-install ] so that ** nvm** will be correctly
326
+ installed to manage multiple installations of ** Node** (with ** npm** )
327
+ on a Windows computer.
272
328
273
329
### Building the project
274
330
@@ -296,20 +352,27 @@ And finally execute the webpack task:
296
352
$ yarn run build
297
353
```
298
354
299
- This command will lint the code with [ ESLint] ( http://eslint.org/docs/user-guide/getting-started ) and transpile the source files from ` src/ ` to ` dist/ ` as an [ UMD] ( http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/ ) with [ Babel] ( https://babeljs.io/ ) . It also generates the minified and source map files.
355
+ This command will lint the code with
356
+ [ ESLint] ( http://eslint.org/docs/user-guide/getting-started )
357
+ and transpile the source files from ` src/ ` to ` dist/ ` as an [ UMD] with
358
+ [ Babel] ( https://babeljs.io/ ) . It also generates the minified and source map
359
+ files.
360
+
361
+ [ ☗ ; Back to Index] ( #content )
300
362
301
363
## Versioning
302
364
303
- This projects adopts the [ Semantic Versioning] ( http://semver.org/ ) (SemVer) guidelines:
365
+ This projects adopts the [ Semantic Versioning] ( http://semver.org/ )
366
+ (SemVer) guidelines:
304
367
305
368
``` text
306
369
<MAJOR>.<MINOR>.<PATCH>
307
370
```
308
371
309
372
Given a version number MAJOR.MINOR.PATCH, increment the:
310
373
311
- 1 . MAJOR version when you make incompatible API changes
312
- 1 . MINOR version when you add functionality in a backwards-compatible manner
374
+ 1 . MAJOR version when you make incompatible API changes.
375
+ 1 . MINOR version when you add functionality in a backwards-compatible manner.
313
376
1 . PATCH version when you make backwards-compatible bug fixes.
314
377
315
378
## Issues
@@ -323,3 +386,12 @@ To report an issue and keep traceability of bug-fixes, please report to:
323
386
This project has been released under the [ ISC] ( https://opensource.org/licenses/ISC ) license.
324
387
This license applies ONLY to the source of this repository and does not extend to any other distribution,
325
388
or any other 3rd party libraries used in a repository. See [ LICENSE] ( LICENSE ) file for more information.
389
+
390
+ <!-- LINKS -->
391
+
392
+ [ UMD ] : http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/
393
+ [ CommonJS ] : https://blog.risingstack.com/node-js-at-scale-module-system-commonjs-require/
394
+ [ ES2015 Export ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
395
+ [ AMD RequireJS ] : http://requirejs.org/docs/api.html#jsfiles
396
+ [ nvm-windows ] : https://github.com/coreybutler/nvm-windows#node-version-manager-nvm-for-windows
397
+ [ nvm-windows-install ] : https://github.com/coreybutler/nvm-windows#installation--upgrades
0 commit comments