1

I've an object as

const posts = {
"BAhvZrRwcfu":{

    "text":"Totally need to try this.",
    "user": "heavymetaladam",
  vote: 2

},
"BAcyDyQwcXX":{

    "text":"Wes. WE should have lunch.",
    "user": "jdaveknox",
  vote : 3

 }
};

is there a way I can sort based on the vote.It seems easier if i make it an array of object

8
  • 1
    Yes, there are possible approaches which can be tried. Can you include the code that you have tried to resolve inquiry at Question? See stackoverflow.com/help/mcve Commented Oct 6, 2017 at 22:09
  • it is not only easier, but it is sortable. Commented Oct 6, 2017 at 22:09
  • @linux See my answer below. You used keys. You wanted values. Commented Oct 6, 2017 at 22:14
  • var keys = Object.keys(state.posts); keys.sort((a, b) => { if (a.voteScore < b.voteScore) { return -1; } if (a.voteScore > b.voteScore) { return 1; } // a.voteScore must be equal to b.voteScore return 0; }).reduce((a, v) => { return state.posts[v] }, {}) Commented Oct 6, 2017 at 22:15
  • 1
    It seems easier if i make it an array of object - yes, you are right on this point, because Objects are not "sortable", arrays are Commented Oct 6, 2017 at 22:30

3 Answers 3

0

I recommend converting to an array of objects. Then you can control your sorting criteria and change them more easily in the future should those criteria change.

You can do something like this:

var postsList = Object.values(posts)

See this Stackoverflow answers for more details on how/why that works:

See previous answer on stack overflow for details on sorting an array of objects, if you don't already know how to do this:

For example, to sort by number of votes:

posts.sort(function(a, b) { return parseInt(a.vote) - parseFloat(b.vote); });

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

2 Comments

Don't use parseInt on numbers!
I finally found a way , I converted to an array of object first then i sorted it,then i converted back to an object of object
0

You can create an array of objects from the original object using Object.entries()

const posts = {
  "BAhvZrRwcfu": {
    "text": "Totally need to try this.",
    "user": "heavymetaladam",
    "vote": 3
  },
  "BAcyDyQwcXX": {
    "text": "Wes. WE should have lunch.",
    "user": "jdaveknox",
    "vote": 2
  }
};

const sortedPosts = Object.entries(posts)
                    .sort(([, {vote: a}], [, {vote: b}]) => a < b ? -1 : 1)
                    .map(([key, prop]) => ({[key]: prop}));

console.log(sortedPosts);

Alternatively you can set the objects within a Map

const posts = {
  "BAhvZrRwcfu": {
    "text": "Totally need to try this.",
    "user": "heavymetaladam",
    "vote": 3
  },
  "BAcyDyQwcXX": {
    "text": "Wes. WE should have lunch.",
    "user": "jdaveknox",
    "vote": 2
  }
};

const sortedPosts = new Map;

Object.entries(posts)
.sort(([, {vote: a}], [, {vote: b}]) => a < b ? -1 : 1)
.forEach(([key, prop]) => sortedPosts.set(key, prop));

console.log(sortedPosts);

1 Comment

this works but it returns an array of object,i need an object of an object.I did this to convert back to an object of an object posts = {} for (let num in aa) { posts[aa[num].id] = aa[num] }
0

Objects are intrinsically key-based and not ordered. To access it in a certain order, I find it helpful to create an order list:

var order = [];
$.each(posts, function(key, value) {
  order.push([key, value.vote])
});  //use for loop if you prefer
order.sort(function(a,b){
  return a[1] - b[1];  //ascending order
})

Then you can access posts based on the order of the array order, e.g.

order.forEach(function(e){
  posts[e[0]];
})

Comments

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.