-2

I have an array which includes some numbers and i wanna sort them by value. For example imagine i have this array:

let myArray = [ 10 , 50 , 30 , 20 , 40]

I wanna sort them by the value from biggest to smallest with for loop or if/else or anything else which i can get result like that:

myArray = [ 50 , 40 , 30 , 20 , 10 ]

or maybe check values and input them in a new array :

newArray = [ 50 , 40 , 30 , 20 , 10 ]

It looks simple but i can't do it,Thanks

Alireza
  • 119
  • 2

4 Answers4

1

There are multiple ways you can achieve this. For what you are wanting at first you would write this:

const myArray = [10, 50, 30, 20, 40];
    myArray.sort((a, b) => b - a);
    console.log(myArray);

to check array value and add it to another array you would write this:

const myArray = [10, 50, 30, 20, 40];
      let newArray = []; //creates an empty array 

      /*this sorts thru the array and for each item in 
      the array it will push it to the newArray from lowest to highest */
      myArray.sort((a, b) => b - a).forEach((item) => 
      newArray.push(item));
      myArray.sort((a, b) => a - b);
      
      console.log(newArray); 
      console.log(myArray);

And if you want to take it a step further you could also sort the values in separate arrays based on if the number is even or odd like so:

       const myArray2 = [2, 4, 11, 8, 28, 23, 26, 13, 30, 33, 81, 37];
       const evenArray = [];
       const oddArray = [];
       myArray2.sort((a, b) => a - b).forEach((item) => {
        if (item % 2 === 0) {
            evenArray.push(item);
        } else {
            oddArray.push(item);
        }
        }
       );
       console.log(evenArray);
       console.log(oddArray);
  1. Create 2 arrays one for even numbers and one for odd.
  2. then sort thru the array
  3. for each number in array, if it is even push it to evenArray
  4. if it is not an even number push it to oddArray then return or in this case console.log()

Hope this helps you understand it a little better!

  • How about object in array? for example: const item1 = { value: 2, name: chris } const item2 = { value: 1, name: alex } const array = [item1 , item2] if i want to sort items by item.value how i can do this? – Alireza Dec 21 '22 at 11:52
  • To sort objects it is the same as above. anytime you need to sort thru an array you just call the "sort() function which is a built in JS function. – Engineer Patterson Dec 21 '22 at 15:39
0

In javascript you can user function

Sort numbers in ascending order:

const points = [40, 100, 1, 5, 25, 10];
points.sort((a, b)=>{return a-b});

Sort numbers in descending order:

const points = [40, 100, 1, 5, 25, 10];
points.sort((a, b)=>{return b-a});

default function is ascending

Ref..https://www.w3schools.com/js/js_array_sort.asp

Zapwoi
  • 51
  • 1
  • 3
0

You can do it in one line:

Ascending: myArray.sort() === [10, 20, 30, 40, 50]

Descending: myArray.sort((a,b)=> b-a) === [50, 40, 30, 20, 10]

Niv Ohayon
  • 19
  • 2
0

Consider utilizing sort() to sort the numbers in descending order in place:

const myArray = [10, 50, 30, 20, 40];
myArray.sort((a, b) => b - a);
console.log(`myArray: [${myArray}]`);

You can sort without mutation of the original array by making a copy before sorting:

const myArray = [10, 50, 30, 20, 40];
const newArray = [...myArray].sort((a, b) => b - a);
console.log(`newArray: [${newArray}]`);
console.log(`myArray: [${myArray}]`);
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40