You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/05-array-methods/article.md
+22-6Lines changed: 22 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -384,7 +384,7 @@ The order became `1, 15, 2`. Incorrect. But why?
384
384
385
385
**The items are sorted as strings by default.**
386
386
387
-
Literally, all elements are converted to strings for comparisons. For strings, lexicographic ordering is applied and indeed `"2" > "15"`.
387
+
Literally, all elements are converted to strings for comparisons. For strings, lexicographic ordering is applied and indeed `"2" > "15"`.
388
388
389
389
To use our own sorting order, we need to supply a function as the argument of `arr.sort()`.
390
390
@@ -431,7 +431,6 @@ By the way, if we ever want to know which elements are compared -- nothing preve
431
431
432
432
The algorithm may compare an element with multiple others in the process, but it tries to make as few comparisons as possible.
433
433
434
-
435
434
````smart header="A comparison function may return any number"
436
435
Actually, a comparison function is only required to return a positive number to say "greater" and a negative number to say "less".
437
436
@@ -456,6 +455,22 @@ arr.sort( (a, b) => a - b );
456
455
This works exactly the same as the longer version above.
457
456
````
458
457
458
+
````smart header="Use `localeCompare` for strings"
459
+
Remember [strings](info:string#correct-comparisons) comparison algorithm? It compares letters by their codes by default.
460
+
461
+
For many alphabets, it's better to use `str.localeCompare` method to correctly sort letters, such as `Ö`.
462
+
463
+
For example, let's sort a few countries in German:
464
+
465
+
```js run
466
+
let countries = ['Österreich', 'Andorra', 'Vietnam'];
467
+
468
+
alert( countries.sort( (a, b) => a > b ?1:-1) ); // Andorra, Vietnam, Österreich (wrong)
469
+
470
+
alert( countries.sort( (a, b) =>a.localeCompare(b) ) ); // Andorra,Österreich,Vietnam (correct!)
471
+
```
472
+
````
473
+
459
474
### reverse
460
475
461
476
The method [arr.reverse](mdn:js/Array/reverse) reverses the order of elements in `arr`.
@@ -530,7 +545,7 @@ The methods [arr.reduce](mdn:js/Array/reduce) and [arr.reduceRight](mdn:js/Array
530
545
The syntax is:
531
546
532
547
```js
533
-
let value =arr.reduce(function(previousValue, item, index, array) {
548
+
let value =arr.reduce(function(accumulator, item, index, array) {
534
549
// ...
535
550
}, [initial]);
536
551
```
@@ -539,14 +554,16 @@ The function is applied to all array elements one after another and "carries on"
539
554
540
555
Arguments:
541
556
542
-
-`previousValue` -- is the result of the previous function call, equals `initial` the first time (if `initial` is provided).
557
+
-`accumulator` -- is the result of the previous function call, equals `initial` the first time (if `initial` is provided).
543
558
-`item` -- is the current array item.
544
559
-`index` -- is its position.
545
560
-`array` -- is the array.
546
561
547
562
As function is applied, the result of the previous function call is passed to the next one as the first argument.
548
563
549
-
Sounds complicated, but it's not if you think about the first argument as the "accumulator" that stores the combined result of all previous execution. And at the end it becomes the result of `reduce`.
564
+
So, the first argument is essentially the accumulator that stores the combined result of all previous executions. And at the end it becomes the result of `reduce`.
565
+
566
+
Sounds complicated?
550
567
551
568
The easiest way to grasp that is by example.
552
569
@@ -611,7 +628,6 @@ let arr = [];
611
628
arr.reduce((sum, current) => sum + current);
612
629
```
613
630
614
-
615
631
So it's advised to always specify the initial value.
616
632
617
633
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.
0 commit comments