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?

7
  • 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. Commented Jul 9, 2020 at 9:11
  • So first, I need to convert my object to an array, then sort the array and then reconvert it to object, right? Commented Jul 9, 2020 at 9:15
  • The idea behind an object being "sorted" in JavaScript is complex and can be implementation-dependent. Best practice dictates if possible, using an array for sorted items and an object if you need to index inside in constant time. Commented Jul 9, 2020 at 9: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. Commented Jul 9, 2020 at 14:03
  • 1
    @FrankvanPuffelen You won't lose the order if the keys are not numeric. See here. Commented Jul 9, 2020 at 14:11

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

I made it with lodash but your solution works too. I respect that. Thank you for your effort. This one is better!
@tpbafk Glad to help.

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.