xxxxxxxxxx
// Sort method
// const arr = [3,53,423,534,3];
// console.log(arr.sort());
// const names = ["fahad", "taha", "salman", "mojeeb"]; // This looks good but if we solve this sort this like with capital letters it'll give first priority to capital letter then it'll sort small letters the example is given below
// names.sort();
// console.log(names);
// Example
// const namesWithCapitalLetters = ["fahad", "Taha", "salman", "mojeeb"];
// namesWithCapitalLetters.sort();
// console.log(namesWithCapitalLetters);
// That's how it works :)
// How to get expected output
// const arr = [3,53,423,534,3];
// console.log(arr.sort()); ---> We're not getting expected output while we're doing this but there is a way with that we can get our expected output
// The way
const arr = [3, 53, 423, 534, 3];
console.log(arr.sort((a, b) => a - b));
// How this is working questing is this?
// What javascript here doing is first javascript 3 and 52 a = 3 b = 52; And now if we 3 to 52 and we'll get number greater than 0 then javascript sort those numbers in order of b then a ---> like 52, 3 and if we got the output less than 0 then javascript sort the number in order of a, b ---> 3 52 and yes you're right this is the correct output :)
// A use case of sort method
// Imagine you have a ecommerce site and you want to sort products prices then how can you sort the prices of products with sort method example given below
// Example
const userCart = [
{ producdId: 1, producdPrice: 355 },
{ producdId: 2, producdPrice: 5355 },
{ producdId: 3, producdPrice: 34 },
{ producdId: 4, producdPrice: 3535 },
];
// Use this for low to high
userCart.sort((a, b) => a.producdPrice - b.producdPrice);
// console.log(userCart)
// ===================================
// Use this for high to low
userCart.sort((a, b) => b.producdPrice - a.producdPrice);
// console.log(userCart)
// ==============The End=================
xxxxxxxxxx
let numbers = [0, 1 , 2, 3, 10, 20, 30 ];
numbers.sort( function( a , b){
if(a > b) return 1;
if(a < b) return -1;
return 0;
});
console.log(numbers);
Code language: JavaScript (javascript)
xxxxxxxxxx
//Sort numbers in ascending order
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a-b});
//Sort numbers in descending order:
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b-a});
//Find the lowest value:
const points = [40, 100, 1, 5, 25, 10];
// Sort the numbers in ascending order
points.sort(function(a, b){return a-b});
let lowest = points[0];
//Find the highest value:
const points = [40, 100, 1, 5, 25, 10];
// Sort the numbers in descending order:
points.sort(function(a, b){return b-a});
let lowest = points[0];
xxxxxxxxxx
function eq(x, y) {
if (x < y) return -1;
else if (x > y) return 1;
else return 0;
}
let num = new Array(8, 50, 2, 34, 12, 8);
num.sort(eq);
let text = num.join();
document.write(text);
xxxxxxxxxx
// Java program to Sort a Subarray in Descending order
// Using Arrays.sort()
// Importing Collections class and arrays classes
// from java.util package
import java.util.Arrays;
import java.util.Collections;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Note that we have Integer here instead of
// int[] as Collections.reverseOrder doesn't
// work for primitive types.
Integer[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
// Sorts arr[] in descending order using
// reverseOrder() method of Collections class
// in Array.sort() as an argument to it
Arrays.sort(arr, Collections.reverseOrder());
// Printing the array as generated above
System.out.println("Modified arr[] : "
+ Arrays.toString(arr));
}
}
xxxxxxxxxx
let products = [
{name: 'iPhone', price: 900},
{name: 'Samsung Galaxy', price: 850},
{name: 'Sony Xperia', price: 700}
];
Code language: JavaScript (javascript)
xxxxxxxxxx
console.log('Products sorted by name:');
products.sort(compareBy('name'));
console.table(products);
Code language: JavaScript (javascript)
xxxxxxxxxx
Products sorted by name:
┌─────────┬──────────────────┬───────┐
│ (index) │ name │ price │
├─────────┼──────────────────┼───────┤
│ 0 │ 'Samsung Galaxy' │ 850 │
│ 1 │ 'Sony Xperia' │ 700 │
│ 2 │ 'iPhone' │ 900 │
└─────────┴──────────────────┴───────┘
Code language: plaintext (plaintext)
xxxxxxxxxx
function compareBy(propertyName) {
return function (a, b) {
let x = a[propertyName],
y = b[propertyName];
if (x > y) {
return 1;
} else if (x < y) {
return -1;
} else {
return 0;
}
};
}
let products = [
{ name: 'iPhone', price: 900 },
{ name: 'Samsung Galaxy', price: 850 },
{ name: 'Sony Xperia', price: 700 },
];
// sort products by name
console.log('Products sorted by name:');
products.sort(compareBy('name'));
console.table(products);
// sort products by price
console.log('Products sorted by price:');
products.sort(compareBy('price'));
console.table(products);
Code language: JavaScript (javascript)
xxxxxxxxxx
// Java program to Sort a Subarray in Descending order
// Using Arrays.sort()
// Importing Collections class and arrays classes
// from java.util package
import java.util.Arrays;
import java.util.Collections;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Note that we have Integer here instead of
// int[] as Collections.reverseOrder doesn't
// work for primitive types.
Integer[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 };
// Sorts arr[] in descending order using
// reverseOrder() method of Collections class
// in Array.sort() as an argument to it
Arrays.sort(arr, Collections.reverseOrder());
// Printing the array as generated above
System.out.println("Modified arr[] : "
+ Arrays.toString(arr));
}
}