2

I have an array of objects like below, and I would like to sort the array by value of each object.


const arr = [
        {
            0: "id1",
            1: {
                title: "object1",
                value: 200
            }
        },
        {
            0: "id2",
            1: {
                title: "object2",
                value: 100
            }
        },
        {
            0: "id3",
            1: {
                title: "object3",
                value: 300
            }
        },
    ]


//result - array I want to get as the final result of sort
sortedArr = [
        {
            0: "id3",
            1: {
                title: "object2",
                value: 100
            }
        },
        {
            0: "id2",
            1: {
                title: "object1",
                value: 200
            }
        },
        {
            0: "id3",
            1: {
                title: "object3",
                value: 300
            }
        },
    ]

I tried:

    const sortedArr = arr.sort((a, b) => {
        if (a[1].value - b[1].value) {
            return -1
        } else {
            return 1
        }             
    })

But I only get the reversed array not sorted as I wish.

I found similar problems but when I tried other solutions it didn't work for me, or I did something wrong.

Piotr
  • 21
  • 2
  • 1
    Why your Object keys are integers `0` `1` ? Seems like a wrong usage of Objects in the first place. Don't treat Objects as a replacement for array. Your data would be better looking like: (instead of arr) `const obj = {id1: {title: "object3", value:200}, id2: {title:"object9", value:100}};` – Roko C. Buljan Jul 01 '23 at 19:55
  • 1
    `if (a[1].value - b[1].value)` makes no sense. You want to just `return a[1].value - b[1].value;` – Bergi Jul 01 '23 at 19:56
  • 1
    Does this answer your question? [Sorting arrays numerically by object property value](https://stackoverflow.com/questions/7889006/sorting-arrays-numerically-by-object-property-value) In your case: `const sortedArr = arr.sort((a, b) => a[1].value - b[1].value)` – Timur Jul 01 '23 at 20:11
  • Why does your expected output have `id3` twice? – Keith Jul 01 '23 at 20:12
  • @Keith I presume it's a copy/paste typo... – Roko C. Buljan Jul 01 '23 at 20:16
  • This is what I get from fetching data from Firebase realtime database. Maybe those keys are not integers but strings. Do you know how can I check that? – Piotr Jul 02 '23 at 10:24

1 Answers1

4

You're misusing data formats I think. Objects are not a replacement for Arrays where keys have to be indexed 0, 1...
Anyways:

const arr = [
  {
    0: "id1",
    1: {title: "object1",value: 200}
  },
  {
    0: "id2",
    1: {title: "object2",value: 100}
  },
  {
    0: "id3",
    1: {title: "object3",value: 300}
  },
];

const sortStrangeInputDataByValue = (a, b) => a["1"].value - b["1"].value;
const sorted = arr.sort(sortStrangeInputDataByValue);
console.log(sorted);

A far better data input given a well structured array of items objects would be:

const arr = [
  {id: "id1", title: "object1", value: 200},
  {id: "id2", title: "object2", value: 100},
  {id: "id3", title: "object3", value: 300}
];


const sortByValue = (a, b) => a.value - b.value;
const sorted = arr.sort(sortByValue);
console.log(sorted);
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • Thank you!! This is my obvious mistake. I should use ["1"] instead of [1]. – Piotr Jul 02 '23 at 10:08
  • You're welcome. Yes, `arr[1]` is used to get an index from array but `obj["1"]` or i.e: `obj["name"]` is the way to get properties values from Objects - in where it's worth knowing that `obj.name` is valid but `obj.1` is not, therefore you need to use square brackets like `obj["1"]` when using integers. – Roko C. Buljan Jul 02 '23 at 10:25