-3

Since there's no official way to sort an object by values, I'm guessing you either (1) Use an array instead or (2) Convert your object to an array using Object.entries(), sort it, then convert back to an object. But option (2) is technically unsafe since Javascript objects aren't supposed to have order.

Now I have a React app where I'm using Redux. I'm storing my data not as an array but as an object iterated by id values. This is what Redux suggests, and I would do it anyways, because of lookup times. I want to sort this redux data, so what I'm currently doing is option (2) of converting to array and then back to object. Which I don't really like.

My question is: Is this what everyone else does? Is it safe to sort an object?

Example:

const sortObject = (obj) => {
  //return sorted object
}

var foo = {a: 234, b: 12, c: 130}
sortObject(foo) // {b: 12, c:130, a:234}

this is what I'm currently doing.

My object data structure looks something like this

obj = {
  asjsd8jsadf: {
    timestamp: 1234432832
  },
  nsduf8h3u29sjd: {
    timestamp: 239084294
  }
}

And this is how I'm sorting it

const sortObj = obj => {
  const objArray = Object.entries(obj);
  objArray.sort((a, b) => {
    return a[1].timestamp < b[1].timestamp ? 1 : -1;
  });
  const objSorted = {};
  objArray.forEach(key => {
    objSorted[key[0]] = key[1];
  });
  return objSorted;
};
S_Farsai
  • 291
  • 1
  • 3
  • 10
  • `Array.sort(comparatorCallback)` would be a convenient way – Psi Dec 12 '17 at 23:49
  • it's not clear what your code is doing, from the description, it seems wrong to me – Jaromanda X Dec 12 '17 at 23:52
  • I can't see the code you're having an issue with, so not sure what you expect. Your description of your code makes it sound like you're doing something wrong, but it's not easy to determine that because you haven't posted any code, but you want to know if it's "best practice" – Jaromanda X Dec 13 '17 at 00:12
  • `ordersArray` is not defined ... so ... – Jaromanda X Dec 13 '17 at 00:31
  • @Tigger that question is about an array of objects. I'm curious about sorting an object itself without converting to an array and without using something like lodash – S_Farsai Dec 13 '17 at 00:31
  • you're wasting your time ... there is no "order" for keys of an object – Jaromanda X Dec 13 '17 at 00:32
  • @JaromandaX that's the point of the question. Given a situation where you have a data structure like I've shared, what's the best approach. Sorry if it's a dumb question. In my situation it makes sense to have an object vs having an array. Especially since I'm using firebase and this is how firebase stores data – S_Farsai Dec 13 '17 at 00:54
  • Best approach to having object keys sorted is not to bother sorting and adding the keys to a new object that you return as object key order is not a thing – Jaromanda X Dec 13 '17 at 00:56

1 Answers1

0

If you are using the Redux documentation for reference you should also have an array with all of the id's in it. Wouldn't it be easier to just sort that array and then use insertion sort when you add something to the state. Then you could use the sorted array to access the byId property of the state?

Dakota
  • 1,254
  • 8
  • 16