Array Sort Methods in JavaScript.!
Published: 2024-07-08 21:24:56

**JavaScriptda Arraylarni saralash usullari.!!**



**Alifbo tartibida saralash.!**
`Array sort()
Array reverse()
Array toSorted()
Array toReversed()`

**Raqamli saralash.!**
`Numeric Sort
Random Sort
Math.min()
Math.max()
Home made Min()
Home made Max()`










<u>1.`sort()`</u>
Ushbu metod Array elementlarini alifbo tartibida saralaydi, asl arrayga ta'sir qiladi.!

```
const fruits = ['grapes','lemon','nut','strawberry','apple','banana'];
console.log(fruits);
//natija - [' grapesgrapes', 'lemon', 'nut','strawberry', 'apple', 'banana']

const fruits_sort = fruits.sort();
console.log(fruits_sort);
//natija - ['apple', 'banana', 'grapes', 'lemon', 'nut', 'strawberry']

console.log(fruits);
//natija - ['apple', 'banana', 'grapes', 'lemon', 'nut', 'strawberry', 'strawberry']
```

<u>2.`reverse()`</u>
Ushbu metod Array elementlarini teskari tartibda joylashtiradi, asl Arrayga tasir qiladi.!

```
const fruits = ['grapes','lemon','nut','strawberry','apple','banana'];
console.log(fruits);
//natija - [' grapesgrapes', 'lemon', 'nut', 'strawberry', 'apple', 'banana']

const fruits_reverse = fruits.reverse();
console.log(fruits_reverse);
//natija - ['banana', 'apple','strawberry', 'nut', 'lemon', 'grapes']

console.log(fruits);
//natija - ['banana', 'apple','strawberry', 'nut', 'lemon', 'grapes']

```


<u>3.`toSorted()`</u>
Ushbu metd ES2023 da qo'shilgan bo'lib `sort()` metodi kabi ishlaydi. Ammo ular orasida bitta farq bor: toSorted() sort() dan farq qilgan xolda aasl Arrayga ta'sir qilmaydi.!

```
const fruits = ['grapes','lemon','nut','strawberry','apple','banana'];
console.log(fruits);
//natija - [' grapesgrapes', 'lemon', 'nut', 'strawberry', 'apple', 'banana']

const fruits_toSorted = fruits.toSorted();
console.log(fruits_toSorted);
//natija - ['apple', 'banana', 'grapes', 'lemon', 'nut', 'strawberry',]

console.log(fruits);
//natija - [' grapesgrapes', 'lemon', 'nut', 'strawberry', 'apple', 'banana']
```

<u>4.`toReversed()`</u>
Ushbu metod ES2023 da qo'shilgan bo'lib reverse() metodi kabi ishlaydi, ammo ular orasida bitta farq bor toReversed() metodi reverse() metodidan farq qilgan xolda asl Arrayga ta'sir qilmaydi.!

```
const fruits = ['grapes','lemon','nut','strawberry','apple','banana'];
console.log(fruits);
//natija - ['grapes', 'lemon', 'nut', 'strawberry', 'apple', 'banana']

const fruits_toReversed = fruits.toReversed();
console.log(fruits_toReversed);
//natija - ['banana', 'apple', 'strawberry', 'nut', 'lemon', 'grapes']

console.log(fruits);
//natija - ['grapes', 'lemon', 'nut', 'strawberry', 'apple', 'banana']
```




**Raqamli saralash.!**

<u>5.`sort()`</u>

Ushbu metod satrlarli Array elementlarini saralashda qulay usul hisoblanadi, raqamlarni saralashda hatoliklarga olibkeladi ammo  ushbu metoddan va `funciya`dan foydalanib raqamlarni saralashimizham mumkin.!

```
const numbers = [3,24,16,12,20,120,100,7];
console.log(numbers);
//natija - [3, 24, 16, 12, 20, 120, 100, 7]

const numbers_sort = numbers.sort();
console.log(numbers_sort);
//natija - [100, 12, 120, 16, 20, 24, 3, 7]

console.log(numbers);
//natija - [100, 12, 120, 16, 20, 24, 3, 7]




const numbersFunc = numbers.sort(
    function numbersF(x,y){return x-y}
);

console.log(numbersFunc);
//natija - [3, 7, 12, 16, 20, 24, 100, 120]
```

Array elementlarini  kamayish usulida saralash.

```
const numbersFunc = numbers.sort(
    function numbersF(x,y){return y-x}
);

console.log(numbersFunc);
//natija - [3, 7, 12, 16, 20, 24, 100, 120]

```



<u>6.`Random Sort`</u>
Ushbu metod array elementlarini tasodifiy usulda saralaydi.!


```
const numbers = [3,24,16,12,20,120,100,7];
console.log(numbers);
//natija - [3, 24, 16, 12, 20, 120, 100, 7]

const numbers_random = numbers.sort(
    function mathRandom(){return 0.5 - Math.random()}
)

console.log(numbers_random);
//natija - [7, 100, 16, 12, 120, 20, 24, 3]
```


<u>7.`Min(), Max()`</u>
`Sort()` yordamida min yoki max qiymatni topish usuli.!

```
//MIN-
const numbers = [3,24,16,12,20,120,100,7];
console.log(numbers);
//natija - [3, 24, 16, 12, 20, 120, 100, 7]

const number_min = numbers.sort(
    function(a,b){
        return a-b;
    }
);

console.log(numbers[0]);
//natija - 3



//MAX-
const numbers = [3,24,16,12,20,120,100,7];
console.log(numbers);
//natija - [3, 24, 16, 12, 20, 120, 100, 7]

const number_min = numbers.sort(
    function(a,b){
        return b-a;
    }
);

console.log(numbers[0])
//natija - 120
```