Skip to content

Commit ddacb54

Browse files
committed
fix: array
1 parent 7991b9e commit ddacb54

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
- [Math & Stats](#math--stats)
5252
- [Integer Division Without Using \* or /](#integer-division-without-using--or-)
5353
- [JavaScript Fundamentals](#javascript-fundamentals)
54+
- [Array Sort](#array-sort)
5455
- [What is an Integer](#what-is-an-integer)
5556
- [increment letters by 3](#increment-letters-by-3)
5657
- [How to convert -ve to +ve number?](#how-to-convert--ve-to-ve-number)
@@ -598,6 +599,40 @@ node .\src\math-and-stats\integer-division.js
598599

599600
## JavaScript Fundamentals
600601

602+
### Array Sort
603+
604+
By default `array.sort` does not work you must pass delegate method.
605+
606+
```js
607+
var a = [23, 4, 2, 155, 43];
608+
a.sort();
609+
// output: [155, 2, 23, 4, 43] Not sorted at all.
610+
```
611+
612+
![](https://i.imgur.com/w9jmc3O.png)
613+
614+
In order to sort in `ascending` order you must pass delegate method.
615+
616+
**Ascending Order Sorting**
617+
618+
```js
619+
var a = [23, 4, 2, 155, 43];
620+
a.sort((a, b) => a - b);
621+
// output: [2, 4, 23, 43, 155]
622+
```
623+
624+
![](https://i.imgur.com/LWQHaAE.png)
625+
626+
**Descending Order Sorting**
627+
628+
```js
629+
var a = [23, 4, 2, 155, 43];
630+
a.sort((a, b) => b - a);
631+
// output: [155, 43, 23, 4, 2]
632+
```
633+
634+
![](https://i.imgur.com/AqYqYyI.png)
635+
601636
### What is an Integer
602637

603638
An integer is a whole number that can be either greater than 0, called positive, or less than 0, called negative. Zero is neither positive nor negative.

0 commit comments

Comments
 (0)