0

I have an object that I would like to sort based on the votes that it has, and then map over the new / sorted object.

const data = {
  "comment-1508872637211" : {
    "description" : "Blah",
    "votes" : 1
  },
  "comment-1508872675949" : {
    "description" : "Question",
    "votes" : 11
  },
  "comment-1508898578089" : {
    "description" : "whatever\n",
    "votes" : 5
  },
  "comment-1508898637092" : {
    "description" : "new",
    "votes" : 15
  },
  "comment-1508900306998" : {
    "description" : "baj",
    "votes" : 0
  }
}

console.log(data);

const sortedOnVotes = Object.entries(data);

console.log(sortedOnVotes);

// This is the part I'm getting hung up on
const newDataObject = sortedOnVotes.map(([key, value]) => value).sort();

console.log(newDataObject)

Ultimately I would love for this new object to still retain the comment-### key and be filtered based on the amount of votes that it has. For example the newDataObject should return something like this:

const newDataObject = {
          "comment-1508900306998" : {
            "description" : "baj",
            "votes" : 0
          },
          "comment-1508872637211" : {
            "description" : "Blah",
            "votes" : 1
          },
          "comment-1508898578089" : {
            "description" : "whatever\n",
            "votes" : 5
          },
          "comment-1508872675949" : {
            "description" : "Question",
            "votes" : 11
          }
          "comment-1508898637092" : {
            "description" : "new",
            "votes" : 15
          }

}

I think I'm on the right track by using Object.values or Object.entries but I'm really getting hung up on it.

Any help would really be appreciated, thanks!

https://codepen.io/MathiasaurusRex/pen/RLzYVV

2
  • javascript object can be sorted, move them to array if you need to sort Commented Oct 25, 2017 at 3:26
  • Use lodash/sort, easy to use and simple to configure. See lodash here. Commented Oct 25, 2017 at 3:35

1 Answer 1

2

You can write the logic to sort the arrays using the sort() function like below-

const data = {
  "comment-1508872637211" : {
    "description" : "Blah",
    "votes" : 1
  },
  "comment-1508872675949" : {
    "description" : "Question",
    "votes" : 11
  },
  "comment-1508898578089" : {
    "description" : "whatever\n",
    "votes" : 5
  },
  "comment-1508898637092" : {
    "description" : "new",
    "votes" : 15
  },
  "comment-1508900306998" : {
    "description" : "baj",
    "votes" : 0
  }
}

console.log(data);

const sortedOnVotes = Object.entries(data);

console.log(sortedOnVotes);

var result = sortedOnVotes.sort(function(a,b) {
  return a[1].votes - b[1].votes;
});

console.log(result);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, looks like this is getting it sorted for me. Im still curious if I could get it back into the same format as it was originally as an object with a key and an object value pair. With your answer it's an array of array.

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.