3
3
<!-- markdownlint-disable MD033 MD034 MD014 -->
4
4
5
5
Sorts an array and allows specify multiple sorting criteria.
6
- It has support for accented characters, and also ignore case sensitive.
6
+ It has support for accented characters, and ignores
7
+ case sensitive to sort strings correctly.
7
8
8
9
## Content
9
10
10
11
1 . [ Getting started] ( #getting-started )
11
12
1 . [ Including the library] ( #including-the-library )
12
13
1 . [ Examples] ( #examples )
13
- 1 . [ Polyfills] ( #polyfills )
14
14
15
15
## Getting started
16
16
@@ -28,17 +28,17 @@ The `sortBy` function has the following signature:
28
28
29
29
``` javascript
30
30
/**
31
- * @param {Array} array : the collection of elements to sort
32
- * @param {Function} parser : transforms each item and specifies the sorting order
31
+ * @param {Array} array : the list of elements to sort
32
+ * @param {Function} parser : (optional) transforms each item and specifies the sorting mode
33
33
* @return {Array}
34
34
*/
35
35
sortBy (array: Array , parser: Function ) : Array
36
36
sortBy (array: Array ) : Array
37
37
```
38
38
39
39
The optional parameter ` parser ` is a function that transforms each element
40
- being iterated and sets the sorting rules: _ ascending_ or _ descending_ , and
41
- the option to specify multiple fields for sorting.
40
+ being iterated and sets the sorting rules: _ ascending_ or _ descending_ .
41
+ Here you can specify the way of sorting by multiple fields .
42
42
43
43
The ` parser ` callback has the following signature:
44
44
@@ -52,6 +52,68 @@ parser(item: Any, index: Number) : Any
52
52
parser (item: Any) : Any
53
53
```
54
54
55
+ Also, a new static method ` mapAccents ` has been added to the ` sortBy ` function.
56
+ This method allows to register a map of accents in order to sort strings correctly.
57
+
58
+ Signature:
59
+
60
+ ``` javascript
61
+ /**
62
+ * @param {String} accents : the string with the accents
63
+ * @param {String} replacements : the replacement for each accent
64
+ */
65
+ sortBy .mapAccents (accents: String , replacements: String ) : void
66
+ ```
67
+
68
+ > Problem solved: when you try order an array of non ASCII characters like this
69
+ ` ['é', 'a', 'ú', 'c'] ` , you will obtain a strange result ` ['c', 'e', 'á', 'ú'] ` .
70
+ That happens because ` .sort() ` does not work correctly with accented characters.
71
+
72
+ By default ` mapAccents ` has an internal mapping with accents and their replacements:
73
+
74
+ ``` javascript
75
+ " ÂâÀàÁáÄäÃãÅåÊêÈèÉéËëÎîÌìÍíÏïÔôÒòÓóÖöÕõÛûÙùÚúÜüÑñÝýÿ"
76
+ " AaAaAaAaAaAaEeEeEeEeIiIiIiIiOoOoOoOoOoUuUuUuUuNnYyy"
77
+ ```
78
+
79
+ To register a new set of special characters you must provide their replacements:
80
+
81
+ ``` javascript
82
+ // register the special characters
83
+ sortBy .mapAccents (
84
+ ' ª@$' ,
85
+ ' aas' ,
86
+ );
87
+
88
+ const arr = [' $impson' , ' Cªl@bazä' , ' M@ría' , ' Cal@bªzA' ];
89
+ sortBy (arr);
90
+ /**
91
+ * expected:
92
+ * ["Cªl@bazä", "Cal@bªzA", "M@ría", "$impson"]
93
+ *
94
+ * translated as:
95
+ * ["CALABAZA", "CALABAZA", "MARIA", "SIMPSON"]
96
+ */
97
+
98
+ sortBy (arr, item => ` DESC:${ item} ` );
99
+ /**
100
+ * expected:
101
+ * ["$impson", "M@ría", "Cal@bªzA", "Cªl@bazä"]
102
+ *
103
+ * translated as:
104
+ * ["SIMPSON", "MARIA", "CALABAZA", "CALABAZA"]
105
+ */
106
+ ```
107
+
108
+ In the example above, after calling ` sortBy.mapAccents() ` we
109
+ added new accents and their replacements at the beginning of
110
+ the internal mapping, honoring the user mapping first:
111
+
112
+ ``` javascript
113
+ " ª@$ÂâÀàÁáÄäÃãÅåÊêÈèÉéËëÎîÌìÍíÏïÔôÒòÓóÖöÕõÛûÙùÚúÜüÑñÝýÿ"
114
+ " aasAaAaAaAaAaAaEeEeEeEeIiIiIiIiOoOoOoOoOoUuUuUuUuNnYyy"
115
+ ```
116
+
55
117
[ ☗ ; Back to Index] ( #content )
56
118
57
119
## Including the library
@@ -61,9 +123,13 @@ parser(item: Any) : Any
61
123
``` html
62
124
<!-- from unpkg.com -->
63
125
<script src =" https://unpkg.com/array-sort-by/dist/sort-by.min.js" ></script >
126
+ <!-- from unpkg.com, including polyfills -->
127
+ <script src =" https://unpkg.com/array-sort-by/dist/sort-by-all.min.js" ></script >
64
128
65
- <!-- or from rawgit.com -->
66
- <script src =" https://cdn.rawgit.com/jherax/array-sort-by/1.1.2/dist/sort-by.min.js" ></script >
129
+ <!-- from rawgit.com -->
130
+ <script src =" https://cdn.rawgit.com/jherax/array-sort-by/1.2.0/dist/sort-by.min.js" ></script >
131
+ <!-- from rawgit.com, including polyfills -->
132
+ <script src =" https://cdn.rawgit.com/jherax/array-sort-by/1.2.0/dist/sort-by-all.min.js" ></script >
67
133
```
68
134
69
135
In the above case, the function [ ` sortBy ` ] ( #examples ) is included as
@@ -130,10 +196,10 @@ sortBy(arr, n => -n);
130
196
*/
131
197
```
132
198
133
- ### Sorting DESC: date-strings as ` Date `
199
+ ### Sorting DESC: date-strings as Date
134
200
135
201
``` javascript
136
- let arr = [" 1983/03/06" , " 1980/12/24" , " 1985/08/31" , " 1983/03/05" ];
202
+ let arr = [' 1983/03/06' , ' 1980/12/24' , ' 1985/08/31' , ' 1983/03/05' ];
137
203
sortBy (arr, (s ) => - new Date (s));
138
204
139
205
/**
@@ -258,44 +324,6 @@ sortBy(arr, item => [item.name, -item.age, item.id]);
258
324
259
325
[ ☗ ; Back to Index] ( #content )
260
326
261
- ## Polyfills
262
-
263
- This library is written using some of the new ES5/ES6 features. If you have
264
- to support Non-standard-compliant browsers like Internet Explorer, you can
265
- polyfill some of the missing features with the following alternatives:
266
-
267
- ** Using [ es6-shim] ( https://github.com/paulmillr/es6-shim ) **
268
-
269
- ``` html
270
- <!-- put this script FIRST, before all other scripts -->
271
- <script src =" https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.min.js" ></script >
272
- ```
273
-
274
- ** Using [ polyfill.io] ( https://polyfill.io/v2/docs/ ) **
275
-
276
- ``` html
277
- <!-- put this script FIRST, before all other scripts -->
278
- <script src =" https://cdn.polyfill.io/v2/polyfill.min.js?features=default-3.3" ></script >
279
- ```
280
-
281
- [ Polyfill.io] ( https://polyfill.io/v2/docs/examples ) reads the ` User-Agent `
282
- header of each request and returns the polyfills that are suitable for the
283
- requesting browser.
284
-
285
- If you want to request specific polyfills, you can pass a query parameter
286
- to the url, for example:
287
-
288
- ``` html
289
- <!-- [if IE]>
290
- <script src="https://polyfill.io/v2/polyfill.min.js?features=default-3.3&flags=always"></script>
291
- <![endif]-->
292
- ```
293
-
294
- Read the list of available features:
295
- [ Features and Browsers Supported] ( https://polyfill.io/v2/docs/features/ ) .
296
-
297
- [ ☗ ; Back to Index] ( #content )
298
-
299
327
## Versioning
300
328
301
329
This projects adopts the [ Semantic Versioning] ( http://semver.org/ )
0 commit comments