|
51 | 51 | - [Math & Stats](#math--stats)
|
52 | 52 | - [Integer Division Without Using \* or /](#integer-division-without-using--or-)
|
53 | 53 | - [JavaScript Fundamentals](#javascript-fundamentals)
|
| 54 | + - [Array Sort](#array-sort) |
54 | 55 | - [What is an Integer](#what-is-an-integer)
|
55 | 56 | - [increment letters by 3](#increment-letters-by-3)
|
56 | 57 | - [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
|
598 | 599 |
|
599 | 600 | ## JavaScript Fundamentals
|
600 | 601 |
|
| 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 | + |
| 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 | + |
| 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 | + |
| 635 | + |
601 | 636 | ### What is an Integer
|
602 | 637 |
|
603 | 638 | 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