1

I'm new to this world of vanilla javascript. I've been trying complete some challenges but it seems like I'm unable to find the solution to a challenge.

The task is:"Sort array by object property"

  1. Write a function that takes an array of objects as an argument
  2. Sort the array by property b in ascending order
  3. Return the sorted array

function arraySorter(arr) {
  return arr
    .map((e) => {
      return Object.values(e);
    })
    .filter((e, i) => {
      console.log(e[1]);
      return;
    });
}

console.log(
  arraySorter([
    { a: 1, b: 2 },
    { a: 5, b: 4 },
  ])
);

Expected output

//expected output: [{a:1,b:2},{a:5,b:4}]
// expected output: [{a:5,b:4},{a:2,b:10}]

4
  • 2
    function arraySorter(arr){ return } ... that returns undefined ... did you forget some code? Commented Jul 23, 2022 at 6:26
  • 3
    stackoverflow.com/help/how-to-ask Commented Jul 23, 2022 at 6:30
  • No @JaromandaX, I was just trying to show the format of the solution I was looking for. This is the furthest I could go- function arraySorter(arr) { return arr .map((e) => { return Object.values(e); }) .filter((e, i) => { console.log(e[1]); return; }); } String.fromCharCode(c.charCodeAt(0) + 1); console.log( arraySorter([ { a: 1, b: 2 }, { a: 5, b: 4 }, ]) ); But I don't think this is it. Commented Jul 23, 2022 at 6:43
  • you should show your attempts in teh question, lest it look like you're using stackoverflow as a homework answering service Commented Jul 23, 2022 at 8:23

2 Answers 2

2

To sort with numbers.

  1. sorting ascending ( first argument - second argument )
  2. sorting descending ( second argument - first argument ),

as sort function should return

  1. positive number if the first argument is bigger than the second argument.
  2. negative number if second argument is bigger than first argument.
  3. zero if both are equal.
    function arraySorter(arr) {
       return arr.sort((x, y) => x.b - y.b)
    }
    
    console.log(arraySorter([{a:1,b:2},{a:5,b:4}])) //expected output: [{a:1,b:2},{a:5,b:4}]
    console.log(arraySorter([{a:2,b:10},{a:5,b:4}])) //Expected output: [{a:5,b:4},{a:2,b:10}]
    console.log(arraySorter([{a:1,b:7},{a:2,b:1}])) //Expected output: [{a:2,b:1},{a:1,b:7}]
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the sort method available on arrays.

You can also make your sort function more generic by accepting the key according to which the objects need to sorted and the order for sorting.

function sortArrayOfObjects(arr, sortKey, isAscending = true) {
  return [...arr].sort(({ [sortKey]: a }, { [sortKey]: b }) =>
    isAscending ? a - b : b - a
  );
}

console.log(sortArrayOfObjects([{ a: 5, b: 2 }, { a: 1, b: 4 }], "a"));
console.log(sortArrayOfObjects([{ a: 2, b: 10 }, { a: 5, b: 4 }], "b"));
console.log(sortArrayOfObjects([{ a: 1, b: 7 }, { a: 2, b: 1 }], "a", false));

2 Comments

But it has two arguments. I'm looking for the solution from a single argument function.
@BhaskarDeb You can hardcode the sortKey or provide a default value, but I'd recommend keeping the function more generic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.