sort() in JS firstly, sort() methods sorts the element on the basis of there Unicode values and not by there face value. That's why we will get wrong answer on sorting more than one digit number. (try it yourself). If we want to sort the number accurately then we need to pass a compare function as an argument in sort() method. Like this: arr.sort( (a, b) => return a-b ); here a is the first value and b is the second value. if a-b = +ve then it means a > b then we need to exchange the position of a with b so the smaller number comes before larger. if a - b = -ve then a < b then it is in correct order. Move to other numbers for check. if a - b = 0 then a == b then it is also in correct order. Now, we need to sort the arr in descending order. there are two ways: 1. arr.sort( (a, b) => return a - b ).reverse(); // by reversing the sorted array. 2. arr.sort( (a, b) => return b-a ); // by reversing the logic // here also a is first number and b is second number if b-a = +ve then it means b > a then we need to exchange the position of b with a so the larger comes before smaller. if b-a = -ve then it means b < a then it is in correct position. if b-a = 0 then it means b == a then it is also in correct order. I hope you got it. if any query ask in comment. Thank you very much for reading it.
//sorting in ascending and descending order let arr=[2,3,5,3,4,7,32,6,1]; arr.sort();//ascending order console.log(arr); arr.sort((a,b)=>b-a);//descending order console.log(arr);
Sorr descending: let numbers = [10, 5, 20, 15]; numbers.sort(function(a, b) { return b - a; }); console.log(numbers); // Output: [20, 15, 10, 5] For words: let words = ["apple", "banana", "grape", "orange"]; words.sort(function(a, b) { return b.localeCompare(a); }); console.log(words); // Output: ["orange", "grape", "banana", "apple"] Find: let numbers = [10, 20, 30, 40, 50]; let result = numbers.find(function(num) { return num > 25; }); console.log(result); // Output: 30 (the first number greater than 25) ForEach: let numbers = [1, 2, 3, 4]; numbers.forEach(function(num) { console.log(num); }); // Output: 1 2 3 4 For..in: let person = { name: "Alice", age: 25, city: "New York" }; for (let key in person) { console.log(key + ": " + person[key]); } // Output: // name: Alice // age: 25 // city: New York For..of: let string = "hello"; for (let char of string) { console.log(char); } // Output: h e l l o
bhaiya i have a doubt that without compare fuction how .sort( ) method works on number array [because it can sort only string value properly] Baaki sab best hai😇😇
37:57 Ascending: let red_array=[11,3,2,8,6]; red_array.sort( (a,b)/*parameters*/=> { return a-b;/*if result is positive 1st number will be placed after 2nd*/ } ) console.log("Ascending Order:",red_array); Descending: red_array.sort( (p,q)/*parameters*/=> q-p /*if result is positive 1st number (p) will be placed after*/ ); console.log("Descending Order:",red_array);
//Find() let arrFind= [23,43,65,87,67,85,67]; console.log(arrFind.find((value)=>{ return value > 50; })); The find() method returns the value of the first element that pass the condition.
Respect for all the teachers and also Love Babbar Sir ❤❤
sort() in JS
firstly, sort() methods sorts the element on the basis of there Unicode values and not by there face value.
That's why we will get wrong answer on sorting more than one digit number. (try it yourself).
If we want to sort the number accurately then we need to pass a compare function as an argument in sort() method.
Like this:
arr.sort( (a, b) => return a-b );
here a is the first value and b is the second value.
if a-b = +ve then it means a > b then we need to exchange the position of a with b so the smaller number comes before larger.
if a - b = -ve then a < b then it is in correct order. Move to other numbers for check.
if a - b = 0 then a == b then it is also in correct order.
Now, we need to sort the arr in descending order.
there are two ways:
1. arr.sort( (a, b) => return a - b ).reverse(); // by reversing the sorted array.
2. arr.sort( (a, b) => return b-a ); // by reversing the logic // here also a is first number and b is second number
if b-a = +ve then it means b > a then we need to exchange the position of b with a so the larger comes before smaller.
if b-a = -ve then it means b < a then it is in correct position.
if b-a = 0 then it means b == a then it is also in correct order.
I hope you got it. if any query ask in comment.
Thank you very much for reading it.
Thanks❤
Bhai tumhe ab CODEHELP ki goodies mil jayegi😂😂😂.
Bhai ab tumhe codehelp ki goodies mil jayegi😂😂😂😂.
Impressive and thanks ❤
Super lecture bhaiya bas bahiya aise hi consistency bna kar complete kr dijiye❤🙏
Crystal Clear Understood🔥🔥
//sorting in ascending and descending order
let arr=[2,3,5,3,4,7,32,6,1];
arr.sort();//ascending order
console.log(arr);
arr.sort((a,b)=>b-a);//descending order
console.log(arr);
Bro reached new topic ,i request please dont stop this series
I have consistently completed html,css and now this js array
Sorr descending:
let numbers = [10, 5, 20, 15];
numbers.sort(function(a, b) {
return b - a;
});
console.log(numbers); // Output: [20, 15, 10, 5]
For words:
let words = ["apple", "banana", "grape", "orange"];
words.sort(function(a, b) {
return b.localeCompare(a);
});
console.log(words); // Output: ["orange", "grape", "banana", "apple"]
Find:
let numbers = [10, 20, 30, 40, 50];
let result = numbers.find(function(num) {
return num > 25;
});
console.log(result); // Output: 30 (the first number greater than 25)
ForEach:
let numbers = [1, 2, 3, 4];
numbers.forEach(function(num) {
console.log(num);
});
// Output: 1 2 3 4
For..in:
let person = { name: "Alice", age: 25, city: "New York" };
for (let key in person) {
console.log(key + ": " + person[key]);
}
// Output:
// name: Alice
// age: 25
// city: New York
For..of:
let string = "hello";
for (let char of string) {
console.log(char);
}
// Output: h e l l o
37:56 arr.sort(function(a, b){return b-a})
What a content bhaiya !Thanks for all this.
sort in inc & dec order
let arr = [4, 1, 2, 9, 0];
let ans = arr.sort();
console.log(ans);
let ans2=arr.sort((a,b)=>b-a)
console.log(ans2);
For the decending order
1. Use sort() function
2. Then use reverse() funstion
🤣🤣🤣🤣🤣🤣
clear
sort() in JS
for sorting in descending order we use "compareFunction" and in compare function
Syntax -: arr.sort((num1, num2) =>{return b-a});
Thank you so much bhaiya ✨
I was just waiting for the next video 👍
sir samaj aya 😃
Great lecture..
you are the best mentor of millions of student and I am the student of supreme 3.O. thanks you bhaiya
amazing lecture bhaiya❤
for sorting in descending form we have to make a comparator let arr=[5,3,2,1]
best teacher ho app bhaiya sb smj m aajata h appka btaya hua
thankyouu bhaiya please series continue rkhna
Thanks For JS Lecture ❤❤
Mazza aa gaya itney achein se function concept explain karney k liye thanl you
Samajh aa gaya bhaiya, dosto go for it is good infinity
bhai yrr kya sahi padhate ho tum!
bhaiya it was super lecture
bhaiya ,kabhi kabhi class ki speed rajdhani express jaisi ho jati hai but samjh bhi a jata hai😇😅
What a nice content. Love it 😍
let arr = [10, 20, 30];
arr.sort((a, b) => b - a); // Sort in descending order
console.log(arr);
samajh me aagaya bhaiya.
Super teaching method ❤
sort array in descending order 38:00
let arr = [9,5,4,3,1,7,6];
arr.sort();
arr.reverse();
console.log(arr);
Awesum lecture brother
Amazing sir
Sort Method by Descending Order
let arr = [1,89,76,90,472,487,920]
let descOrder = arr.sort((a,b) =>{
return b - a;
})
console.log(descOrder);
Samaj aagya sara Shurkiyaa
understtood everything
nice lecture
javascript me array me diiferent types ke data bhi support krte h?
Great lecture❤❤
love bhai you are video consistently in all doubt clear js
Thank You Sir ❤❤one of the best tutor on youtube
bahoot ache se smh aaya hai bhai
Love babar bhaiya Eska bad please Devops ka bhi course start kr skta hain kya ap
method to sort an array in descending order is let arr = [7,8,9,4,5,6,1,3,2]
arr.sort ( ( a , b ) => b - a );
console.log(arr)
always have a good learning exp.
understood bhaiya, thanks
Clear Understood
best course😎😎😎
yes bhaiya aa gaya
nicely explained 👍
I subscribed the 3.0 and waiting for the 25 th April first class 🎉🎉🎉
ok
nice1000%good containt
best course
samaj gya bhaiya, thank u :)
There is one suggestion from me. You should give some questions for practice
Let me complete basics uske baad will do
maza aa gya bhaiya
great lecture
Babbar bhaiya kindly kuch practice ky liay resources btayn tky class ky baad code kr sakyn any Exercises for practice please
samj aa gya bhaiya 🙂
awesome content bro
bhaiya i have a doubt that without compare fuction how .sort( ) method works on number array [because it can sort only string value properly]
Baaki sab best hai😇😇
Thank you Bhaiyyaa❤❤
amazing content 🔥🔥🔥
well explained
Samaz gaya Sir
great content
great work
Mja aa gya love bhaiya... ❤️❤️
37:57
Ascending:
let red_array=[11,3,2,8,6];
red_array.sort( (a,b)/*parameters*/=>
{
return a-b;/*if result is positive 1st number will be placed after 2nd*/
}
)
console.log("Ascending Order:",red_array);
Descending:
red_array.sort( (p,q)/*parameters*/=>
q-p /*if result is positive 1st number (p) will be placed after*/
);
console.log("Descending Order:",red_array);
keep it up bhaiya !!
What a consistency ❤❤
Thank you very much, bhaiya
30:33 😂 ye shi tha error ko btane ka 😂😂😂.
by using reverse method we can print descending order
arr.sort(); // for ascending
arr.sort().reverse(); // for descending
better than apna collge ❤❤❤❤❤❤
Thanks sir g always happy
smjh aagya
thank you sir . i did fill better your class, and your class is helpfull for me
Bhiaya❤❤❤❤❤❤Sam smjh me aagya
Explain
bhaiya mazza aagya naya lecture lao web dev ka
understood ++;
bhaiya waiting for next video
aise hi aur videos banate rho
Wow bhaiya you are great ❤❤❤❤
BHAIYA THANKS FOR VISITING NIT KKR TODAY ❤
//Find()
let arrFind= [23,43,65,87,67,85,67];
console.log(arrFind.find((value)=>{
return value > 50;
}));
The find() method returns the value of the first element that pass the condition.
Thanks!!
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const dec = (a , b) => {
return b-a;
};
const ans = arr.sort(dec);
console.log(ans);
let arr= [1,9,3,2,5,6];
console.log(arr.sort());
console.log(arr.reverse());
bhaiya aur bhi lectures hain kya JS ke..iske baad
Bhaiya good video 😅😅😅❤
hard hai pr aap bhaut clear padha rahe ho with time aasan ho jye ga
Maza a gaya sir 🙏🏻🙏🏻
Best video
please provide DSA with java course .
GREAT
Hi respected sir , kindly create some web development projects using mern stack beginner to advance level
The way you explain "reduce method" 🤌❤
Sir please complete this series 🙏🏻
wao bhiya maja aa gya 🥰🥰🥰🥰🥰🥰😍