1

I have a JSON file like this:

const presets = {
  key1: {
    val1: "asd",
    val2: "dsadd",
    singles: {
      likeCount: 23,
    },
  },
  key2: {
    val1: "asd2",
    val2: "dsadd2",
    singles: {
      likeCount: 100,
    },
  },
  key3: {
    val1: "asd3",
    val2: "dasad3",
    singles: {
      likeCount: 15,
    },
  },
  key4: {
    val1: "asd3",
    val2: "dasad3",
    singles: {
      likeCount: 80,
    },
  },
}

And I want to order this file by the likeCount property

Here's what I tried:

Object.keys(presets)
  .map((key) => Object.assign({ key }, presets[key]))
  .sort((a, b) => b.singles.likeCount - a.singles.likeCount)
  .every((preset, index) => {
    fs.appendFile("./data.json", JSON.stringify(preset), () => {})
    return index < 20
  })

and the output:

[
  {
    "key": "key2",
    "val1": "asd2",
    "val2": "dsadd2",
    "singles": {
      "likeCount": 100
    }
  },
  {
    "key": "key4",
    "val1": "asd3",
    "val2": "dasad3",
    "singles": {
      "likeCount": 80
    }
  },
  {
    "key": "key1",
    "val1": "asd",
    "val2": "dsadd",
    "singles": {
      "likeCount": 23
    }
  },
  {
    "key": "key3",
    "val1": "asd3",
    "val2": "dasad3",
    "singles": {
      "likeCount": 15
    }
  }
]

The expected output:

{
  "key2": {
    "val1": "asd2",
    "val2": "dsadd2",
    "singles": {
      "likeCount": 100
    }
  },
  "key4": {
    "val1": "asd3",
    "val2": "dasad3",
    "singles": {
      "likeCount": 80
    }
  },
  "key1": {
    "val1": "asd",
    "val2": "dsadd",
    "singles": {
      "likeCount": 23
    }
  },
  "key3": {
    "val1": "asd3",
    "val2": "dasad3",
    "singles": {
      "likeCount": 15
    }
  }
}

My file's size is 80MB and after I sorted the data, file new file's size becomes 50MB. I mean, some of the data is also missing after the sorting.

My question: How can I sort an object like that without losing or changing anything on the data?

iota
  • 34,586
  • 7
  • 32
  • 51
tpbafk
  • 388
  • 2
  • 17
  • 1
    In JavaScript, you're not able to order keys in an object, which is what it looks like in the expected output. A function that is a sorted list by some value will have to be an array of objects as is your current output. – rb612 Jul 09 '20 at 09:11
  • So first, I need to convert my object to an array, then sort the array and then reconvert it to object, right? – tpbafk Jul 09 '20 at 09:15
  • The idea behind an object being "sorted" in JavaScript is complex and can be [implementation-dependent](https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key). Best practice dictates if possible, using an array for sorted items and an object if you need to index inside in constant time. – rb612 Jul 09 '20 at 09:19
  • 1
    You can definitely sort the data in an array, but if you then convert it back to a regular object, you'll lost the sort order again. – Frank van Puffelen Jul 09 '20 at 14:03
  • @rb612 Keys are iterated over in insertion order if they are not numeric. The question you linked to says this as well. – iota Jul 09 '20 at 14:06
  • 1
    @FrankvanPuffelen You won't lose the order if the keys are not numeric. See [here](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order). – iota Jul 09 '20 at 14:11
  • @FrankvanPuffelen and @ hev1 good to know. Thanks! – tpbafk Jul 09 '20 at 17:32

1 Answers1

1

You can sort Object.entries and convert it back to an object with Object.fromEntries as long as none of the keys are numeric, in which case they will be iterated over in insertion order.

const presets = {
  key1: {
    val1: "asd",
    val2: "dsadd",
    singles: {
      likeCount: 23,
    },
  },
  key2: {
    val1: "asd2",
    val2: "dsadd2",
    singles: {
      likeCount: 100,
    },
  },
  key3: {
    val1: "asd3",
    val2: "dasad3",
    singles: {
      likeCount: 15,
    },
  },
  key4: {
    val1: "asd3",
    val2: "dasad3",
    singles: {
      likeCount: 80,
    },
  },
}
const res = Object.fromEntries(
     Object.entries(presets)
       .sort(([k1,v1],[k2,v2])=>v2.singles.likeCount - v1.singles.likeCount));
console.log(res);
iota
  • 34,586
  • 7
  • 32
  • 51