Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sort method

// 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=================
Comment

sort() function example JavaScript

function ascendingOrder(arr) {
  return arr.sort(function(a, b) {
    return a - b;
  });
}
ascendingOrder([1, 5, 2, 3, 4]);
Comment

Sort() functions

//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];
Comment

sort function

a=[2,2,4,1]
b=a
a.sort()
// a now points to object [1,2,2,4]
c=sorted(b)
//c and b also points to [1,2,2,4] 
// sort works on array only but sorted also on strings but return array of char
s="sjndk"
print(sorted(s))
// prints ['d', 'j', 'k', 'n', 's']
// sorted also works on list of strings(sorts alphabetically)
Comment

sort function explained javascript

var sortedArray = myArray.sort(function(a,b){
                                   if (a.name < b.name)
                                      return -1;
                                   else if (a.name == b.name)
                                      return 0;
                                   else
                                      return 1;
                               });
Comment

sort array method

//The sort() method sorts an array alphabetically:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
//output >> [ "Apple",Banana","Mango", "Orange" ]

//if you find the answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
Comment

PREVIOUS NEXT
Code Example
Javascript :: randomize an array 
Javascript :: scroll btn 
Javascript :: scroll up own 
Javascript :: javascript /g 
Javascript :: sum of two array in javascript 
Javascript :: export json / array to excel in javascript 
Javascript :: ordenar numeros array javascript 
Javascript :: sort array of numbers js 
Javascript :: axios js 
Javascript :: for in loop javascript 
Javascript :: angular map 
Javascript :: next js get gurrent page params 
Javascript :: add font awesome with nextjs 
Javascript :: gif as animation react 
Javascript :: array==null array.length java script 
Javascript :: are you sure you want to close this window javascript 
Javascript :: moment.js format 
Javascript :: npm md to html 
Javascript :: moment js 
Javascript :: shadow react native generator 
Javascript :: redux action 
Javascript :: first node prog using express 
Javascript :: bootstrap carousel dynamic height jquery 
Javascript :: router nodejs 
Javascript :: spawn prop with custom model 
Javascript :: auto increase hight of textarea with alpine js 
Javascript :: js map delete item 
Javascript :: how to copy object in javascript 
Javascript :: rivets js bind 
Javascript :: react native azure 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =