1- # Array sort-by
1+ # Array sortBy
22
33<!-- markdownlint-disable MD033 MD034 MD014 -->
44
5- The motivation to create this function was to provide a mechanism that allow sort lists of elements,
6- with the possibility to specify multiple sort criteria.
5+ The motivation for creating this utility was to provide a mechanism to organize elements,
6+ with the ability to specify multiple ordering criteria.
77
88## Getting started
99
@@ -45,10 +45,12 @@ function parser(item, index);
4545`array-sort-by` can be included directly from a CDN in your page:
4646
4747```html
48- <!-- last version: 1.0.1 -->
49- <script src="https:// cdn.rawgit.com/jherax/array-sort-by/1.0.1 /dist/sort-by.min.js"></script>
48+ <!-- last version: 1.0.2 -->
49+ <script src="https:// cdn.rawgit.com/jherax/array-sort-by/1.0.2 /dist/sort-by.min.js"></script>
5050```
5151
52+ In the above case, the function [`sortBy`](#examples) is included as a global object in the browser.
53+
5254As this library is built as an [UMD](http:// davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/)
5355(Universal Module Definition), it can be included from a module loader as AMD, CommonJS, or ES2015 Export.
5456
@@ -67,16 +69,20 @@ import sortBy from 'array-sort-by';
6769### AMD
6870
6971```javascript
72+ // using RequireJS
7073requirejs.config({
7174 paths: {
75+ // remove the extension .js
7276 ' array-sort-by' : ' <PATH>/sort-by.min'
7377 }
7478});
75- define ([' array-sort-by' ], function (sortBy ) {
79+ require ([' array-sort-by' ], function (sortBy ) {
7680 sortBy (someArray);
7781});
7882```
7983
84+ See an example with RequireJS here: http://jsfiddle.net/FdKTn/66/
85+
8086## Examples
8187
8288### Default sorting (ASC)
@@ -103,7 +109,7 @@ sortBy(arr, n => -n);
103109 */
104110```
105111
106- ### Sorting DESC date-strings in format ` 'yyyy/MM/dd' ` as ` Date `
112+ ### Sorting DESC date-strings as ` Date `
107113
108114``` javascript
109115let arr = [" 1983/03/06" , " 1980/12/24" , " 1985/08/31" , " 1983/03/05" ];
@@ -133,21 +139,21 @@ sortBy(arr, (s) => "desc:" + s);
133139
134140``` javascript
135141let arr = [
136- { n : 8 , d: " 1985/08/31" },
137- { n : 2 , d: " 1980/12/24" },
138- { n : 5 , d: " 1983/03/06" },
139- { n : 8 , d: " 1983/03/06" }
142+ { a : 8 , d: " 1985/08/31" },
143+ { a : 2 , d: " 1980/12/24" },
144+ { a : 5 , d: " 1983/03/06" },
145+ { a : 8 , d: " 1983/03/06" }
140146];
141147
142- sortBy (arr, (o ) => [- o .n , new Date (o .d )]);
148+ sortBy (arr, (o ) => [- o .a , new Date (o .d )]);
143149
144150/*
145151 * expected:
146152 * [
147- * { n : 8, d: "1983/03/06" },
148- * { n : 8, d: "1985/08/31" },
149- * { n : 5, d: "1983/03/06" },
150- * { n : 2, d: "1980/12/24" }
153+ * { a : 8, d: "1983/03/06" },
154+ * { a : 8, d: "1985/08/31" },
155+ * { a : 5, d: "1983/03/06" },
156+ * { a : 2, d: "1980/12/24" }
151157 * ]
152158 */
153159```
@@ -179,21 +185,21 @@ sortBy(arr, (o) => "DESC:" + o.name.toUpperCase());
179185
180186``` javascript
181187let arr = [
182- { id : 4 , name : " pedro " , age : 26 },
183- { id : 6 , name : " Pedro " , age : 32 },
184- { id : 7 , name : " Luis " , age : 26 },
185- { id : 2 , name : " luis " , age : 26 }
188+ { a : 4 , age : 26 , name : " pedro " },
189+ { a : 6 , age : 32 , name : " Pedro " },
190+ { a : 7 , age : 26 , name : " Luis " },
191+ { a : 2 , age : 26 , name : " luis " }
186192];
187193
188- sortBy (arr, (o ) => [o .name .toUpperCase (), - o .age , o .id ]);
194+ sortBy (arr, (o ) => [o .name .toUpperCase (), - o .age , o .a ]);
189195
190196/*
191197 * expected:
192198 * [
193- * { id : 2, name: "luis", age: 26 },
194- * { id : 7, name: "Luis", age: 26 },
195- * { id : 6, name: "Pedro", age: 32 },
196- * { id : 4, name: "pedro", age: 26 }
199+ * { a : 2, age: 26, name: "luis" },
200+ * { a : 7, age: 26, name: "Luis" },
201+ * { a : 6, age: 32, name: "Pedro" },
202+ * { a : 4, age: 26, name: "pedro" }
197203 * ]
198204 */
199205```
@@ -204,15 +210,22 @@ If you want to fork or build your own, you must run this project.
204210
205211### Requirements
206212
207- 1 . Git ([ git-linux] ( https://git-scm.com/book/en/v2/Getting-Started-Installing-Git ) or [ git-windows ] ( https://git-for-windows.github.io/ ) ).
208- 1 . [ Node.js ] ( https://nodejs.org/en/ ) (latest stable version v6+ ).
209- 1 . Node Package Manager ( [ npm ] ( https://docs.npmjs.com/ ) v3+), the one that comes with your ** node.js ** version.<br >
210- It is preferable to install Node Version Manager - ** [ nvm] ( https://github.com/creationix/nvm ) ** , it contains both
211- ** node.js ** and ** npm ** .
213+ 1 . Git ([ git-linux] ( https://git-scm.com/book/en/v2/Getting-Started-Installing-Git )
214+ or [ git-windows ] ( https://git-for-windows.github.io/ ) ).
215+ 1 . [ Node.js ] ( https://nodejs.org/en/ ) (latest stable version v6+) .<br >
216+ It is preferable install ** [ nvm] ( https://github.com/creationix/nvm ) **
217+ (Node Version Manager) .
2122181 . [ Yarn] ( https://yarnpkg.com/en/docs/cli/ ) installed as a global package.
213219
214- ** NOTE** : Consider to install Node Version Manager (** NVM** ) to upgrade easily the Node.js version.<br >
215- Go to https://github.com/creationix/nvm and check the installation process for your OS.
220+ ** NOTE** : Consider install Node Version Manager (** nvm** ) to upgrade easily the NodeJS version.
221+ <br >Go to https://github.com/creationix/nvm and check the installation process for your OS.
222+
223+ If you are running Windows, you can install
224+ [ nvm-windows] ( https://github.com/coreybutler/nvm-windows#node-version-manager-nvm-for-windows ) .
225+ Follow every step mentioned
226+ [ here] ( https://github.com/coreybutler/nvm-windows#installation--upgrades )
227+ so that nvm will be correctly installed to manage multiple installations
228+ of ** node.js** (with ** npm** ) on a Windows computer.
216229
217230### Building the project
218231
@@ -240,9 +253,7 @@ And finally execute the webpack task:
240253$ yarn run build
241254```
242255
243- This command will lint the code with [ ESLint] ( http://eslint.org/docs/user-guide/getting-started ) and after that
244- it will transpile with [ Babel] ( https://babeljs.io/ ) the ES2015 Module in ` src/ ` folder to an UMD ES5 Module in ` dist/ `
245- and finally it will generate the minified and source map files.
256+ 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.
246257
247258## Versioning
248259
0 commit comments