DEV Community

Cover image for Array Sort Methods in JavaScript.!
Hodiev
Hodiev

Posted on • Edited on

Array Sort Methods in JavaScript.!

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()

1.sort()
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']
Enter fullscreen mode Exit fullscreen mode

2.reverse()
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']

Enter fullscreen mode Exit fullscreen mode

3.toSorted()
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']
Enter fullscreen mode Exit fullscreen mode

4.toReversed()
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']
Enter fullscreen mode Exit fullscreen mode

Raqamli saralash.!

5.sort()

Ushbu metod satrlarli Array elementlarini saralashda qulay usul hisoblanadi, raqamlarni saralashda hatoliklarga olibkeladi ammo ushbu metoddan va funciyadan 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]
Enter fullscreen mode Exit fullscreen mode

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]

Enter fullscreen mode Exit fullscreen mode

6.Random Sort
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]
Enter fullscreen mode Exit fullscreen mode

7.Min(), Max()
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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)