-1
Var obj = [
{band: “3”, name: “positive”},
{band: “-3”, name: “positive”}
{band: “1”, name: “positive”},
{band: “4”, name: “positive”},
{band: “-2”, name: “positive”},
{band: “2”, name: “positive”}
];

Expecting output like this below. It should be ordered based on band.

Var obj = [
{band: “4”, name: “positive”},
{band: “3”, name: “positive”}
{band: “-3”, name: “positive”},
{band: “2”, name: “positive”},
{band: “-2”, name: “positive”},
{band: “1”, name: “positive”}
];
KRISH JACKMAN
  • 25
  • 1
  • 10
  • Did you try to solve this? Add your code so we can help. Also, [better](https://stackoverflow.com/questions/50335497/can-i-completely-stop-using-var) not to use `var` any more. – jarmod Jul 22 '22 at 16:41
  • 1
    Did you copy&paste this homework from Word or where do these strange "quotes" come from? – Andreas Jul 22 '22 at 16:43
  • [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – Andreas Jul 22 '22 at 16:45

1 Answers1

1

In the sort callback, get the absolute value of the object's band property:

const obj=[{band:"3",name:"positive"},{band:"-3",name:"positive"},{band:"1",name:"positive"},{band:"4",name:"positive"},{band:"-2",name:"positive"},{band:"2",name:"positive"}];

const result = obj.sort((a,b) => Math.abs(b.band) - Math.abs(a.band))
console.log(result)
Spectric
  • 30,714
  • 6
  • 20
  • 43