-2

Let's say I have following JSON:

[
    {
        "name": "John",
        "value": 10
    },
    {
        "name": "David",
        "value": 1000
    },
    {
        "name": "Damian",
        "value": 100
    }
]

I want to check it and sort it, so the end result will be this:

[
    {
        "name": "David",
        "value": 1000
    },
    {
        "name": "Damian",
        "value": 100
    }
    {
        "name": "John",
        "value": 10
    },
]

How can I do that?

Gucci
  • 13
  • 2
  • 3
  • 2
    What does "check it" mean? On which property do you want to sort? Have you done any research into solving this yourself? Have you made any attempts to solve this yourself? – Heretic Monkey Mar 05 '20 at 18:42

1 Answers1

-1

let data = [
    {
        "name": "John",
        "value": 10
    },
    {
        "name": "David",
        "value": 1000
    },
    {
        "name": "Damian",
        "value": 100
    }
]

const result = data.sort((a, b) => a.value < b.value);

console.log(result);
Chris L
  • 129
  • 5
  • 1º: your solution logs a different value than the one that OP wants. 2º: Question already have many duplicated ones in Stackoverflow, so it can be flagged as duplicated and don't need answer, OP just need to research a little bit – Calvin Nunes Mar 05 '20 at 18:56
  • How does it log a different solution? It orders the array of objects by value descending. I didn't see that it was marked as duplicate until after I already posted it. – Chris L Mar 05 '20 at 18:58
  • 1
    10, 1000, 100 are not ordered. – Philip Mar 05 '20 at 19:01
  • You have to hit "run code snippet" to see the execution of my code... – Chris L Mar 05 '20 at 19:01
  • as stated, it is not ordered, it displays: 10, 1000, 100... yes, in the console.log after hit run – Calvin Nunes Mar 05 '20 at 19:02
  • Calvin, the data inside 'result' is now ordered. I simply log it so you can see the data. I don't know how to be any clearer. – Chris L Mar 05 '20 at 19:04
  • Me also, because your array **is not** ordered, you are logging `result` (the log shows those values: 10, 1000, 100) this **is not** ordered as expected (that is 1000, 100, 10)... You can see it in the console.log... but anyway, the answer is yours, you do what you want with it – Calvin Nunes Mar 05 '20 at 19:06
  • Well my issue was that sort returns a value not a boolean. You should be using `-` not `<` – Philip Mar 05 '20 at 19:06
  • Buddy, result is a sorted array sorted by the 'value' property in descending order. – Chris L Mar 05 '20 at 19:07