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

linux
  • 141
  • 2
  • 12
  • 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 https://stackoverflow.com/help/mcve – guest271314 Oct 06 '17 at 22:09
  • it is not only easier, but it is sortable. – Nina Scholz Oct 06 '17 at 22:09
  • @linux See my answer below. You used keys. You wanted values. – TinkerTenorSoftwareGuy Oct 06 '17 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] }, {})` – linux Oct 06 '17 at 22:15
  • `"voteScore"` does not appear as a property of an object at code at Question. Can you include the code that you tried at text of Question? Note, there are mismatched quotation marks at `'BAcyDyQwcXX"` – guest271314 Oct 06 '17 at 22:15
  • The object at OP is not valid at `"BAhvZrRwcfu":{{`, do you mean `{"BAhvZrRwcfu":{"text":"Totally need to try this.","user":"heavymetaladam","vote":2},"BAcyDyQwcXX":{"text":"Wes. WE should have lunch.","user":"jdaveknox","vote":3}}`? – guest271314 Oct 06 '17 at 22:21
  • yes,I've corrected the question – linux Oct 06 '17 at 22:28
  • 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 – Jaromanda X Oct 06 '17 at 22:30

3 Answers3

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

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);
guest271314
  • 1
  • 15
  • 104
  • 177
  • 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] }` – linux Oct 06 '17 at 23:02
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]];
})
EJAg
  • 3,210
  • 2
  • 14
  • 22