22
33<!-- markdownlint-disable MD033 MD034 MD014 -->
44
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 .
77
88## Content
99
10101 . [ Getting started] ( #getting-started )
11111 . [ Including the library] ( #including-the-library )
12121 . [ Examples] ( #examples )
13- 1 . [ Shimming-polyfills ] ( #shimming- polyfills )
13+ 1 . [ Polyfills ] ( #polyfills )
14141 . [ Running the project] ( #running-the-project )
1515
1616## Getting started
@@ -25,41 +25,54 @@ $ npm install array-sort-by --save
2525$ yarn add array-sort-by
2626```
2727
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:
2929
3030``` 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
3434 * @return {Array}
3535 */
36- function sortBy (array , /* optional*/ parser );
36+ sortBy (array: Array , parser: Function ) : Array
37+ sortBy (array: Array ) : Array
3738```
3839
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:
4145
4246``` javascript
43- /*
47+ /**
4448 * @param {Any} item : the element being iterated over the list
4549 * @param {Number} index : the index of the element in the list
4650 * @return {Any}
4751 */
48- function parser(item, index);
52+ parser (item: Any, index: Number ) : Any
53+ parser (item: Any) : Any
4954```
5055
56+ [ ☗ ; Back to Index] ( #content )
57+
5158## Including the library
5259
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 :
5461
5562``` 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 >
5768```
5869
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.
6072
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] .
6376
6477### CommonJS
6578
@@ -88,7 +101,9 @@ require(['array-sort-by'], function(sortBy) {
88101});
89102```
90103
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 )
92107
93108## Examples
94109
@@ -98,7 +113,7 @@ See an example with RequireJS here: http://jsfiddle.net/FdKTn/69/
98113let arr = [10 , 8 , 5 , 3 , 0 , 7 , 4 , 5 , 1 ];
99114sortBy (arr);
100115
101- /*
116+ /**
102117 * expected:
103118 * [0, 1, 3, 4, 5, 5, 7, 8, 10]
104119 */
@@ -110,7 +125,7 @@ sortBy(arr);
110125let arr = [5 , 1 , 8 , 0 , 3 , 7 , 10 , 4 , 3 , 8 ];
111126sortBy (arr, n => - n);
112127
113- /*
128+ /**
114129 * expected:
115130 * [10, 8, 8, 7, 5, 4, 3, 3, 1, 0]
116131 */
@@ -122,7 +137,7 @@ sortBy(arr, n => -n);
122137let arr = [" 1983/03/06" , " 1980/12/24" , " 1985/08/31" , " 1983/03/05" ];
123138sortBy (arr, (s ) => - new Date (s));
124139
125- /*
140+ /**
126141 * expected:
127142 * ["1985/08/31", "1983/03/06", "1983/03/05", "1980/12/24"]
128143 */
@@ -136,12 +151,50 @@ Because we use the minus **(-)** symbol to specify a descending order, it will p
136151let arr = [" 1983/03/06" , " 1980/12/24" , " 1985/08/31" , " 1983/03/05" ];
137152sortBy (arr, (s ) => " desc:" + s);
138153
139- /*
154+ /**
140155 * expected:
141156 * ["1985/08/31", "1983/03/06", "1983/03/05", "1980/12/24"]
142157 */
143158```
144159
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+
145198### Sorting DESC by @a , after ASC by @d (as Date)
146199
147200``` javascript
@@ -154,7 +207,7 @@ let arr = [
154207
155208sortBy (arr, (o ) => [- o .a , new Date (o .d )]);
156209
157- /*
210+ /**
158211 * expected:
159212 * [
160213 * { a: 8, d: "1983/03/06" },
@@ -165,53 +218,55 @@ sortBy(arr, (o) => [-o.a, new Date(o.d)]);
165218 */
166219```
167220
168- ### Sorting DESC by @name (ignore case sensitive)
221+ ### Sorting DESC by @name
169222
170223``` javascript
171224let arr = [
172225 { id: 4 , name: " Pedro" },
173- { id: 6 , name: " Lucia " },
226+ { id: 6 , name: " Lucía " },
174227 { id: 7 , name: " paco" },
175228 { id: 3 , name: " luis" }
176229];
177230
178- sortBy (arr, ( o ) => " DESC:" + o .name . toUpperCase () );
231+ sortBy (arr, item => " DESC:" + item .name );
179232
180- /*
233+ /**
181234 * expected:
182235 * [
183236 * { id: 4, name: "Pedro" },
184237 * { id: 7, name: "paco" },
185238 * { id: 3, name: "luis" },
186- * { id: 6, name: "Lucia " }
239+ * { id: 6, name: "Lucía " }
187240 * ]
188241 */
189242```
190243
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
192245
193246``` javascript
194247let arr = [
195248 { a: 4 , age: 26 , name: " pedro" },
196249 { 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 " }
199252];
200253
201- sortBy (arr, ( o ) => [o .name . toUpperCase () , - o .age , o .a ]);
254+ sortBy (arr, item => [item .name , - item .age , item .a ]);
202255
203- /*
256+ /**
204257 * expected:
205258 * [
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 " },
208261 * { a: 6, age: 32, name: "Pedro" },
209262 * { a: 4, age: 26, name: "pedro" }
210263 * ]
211264 */
212265```
213266
214- ## Shimming-polyfills
267+ [ ☗ ; Back to Index] ( #content )
268+
269+ ## Polyfills
215270
216271This library is written using some of the new ES5/ES6 features. If you have
217272to support Non-standard-compliant browsers like Internet Explorer, you can
@@ -247,28 +302,29 @@ to the url, for example:
247302Read the list of available features:
248303[ Features and Browsers Supported] ( https://polyfill.io/v2/docs/features/ ) .
249304
305+ [ ☗ ; Back to Index] ( #content )
306+
250307## Running the project
251308
252309If you want to fork or build your own, you must run this project.
253310
254311### Requirements
255312
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/ ) .
2583151 . [ 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.
262319
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.
265323
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.
272328
273329### Building the project
274330
@@ -296,20 +352,27 @@ And finally execute the webpack task:
296352$ yarn run build
297353```
298354
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 )
300362
301363## Versioning
302364
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:
304367
305368``` text
306369<MAJOR>.<MINOR>.<PATCH>
307370```
308371
309372Given a version number MAJOR.MINOR.PATCH, increment the:
310373
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.
3133761 . PATCH version when you make backwards-compatible bug fixes.
314377
315378## Issues
@@ -323,3 +386,12 @@ To report an issue and keep traceability of bug-fixes, please report to:
323386This project has been released under the [ ISC] ( https://opensource.org/licenses/ISC ) license.
324387This license applies ONLY to the source of this repository and does not extend to any other distribution,
325388or 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