-3

I have an object with a id being the key and array[4] value

myArray = { 
            5bb4c21d2d49b2223066cf62:[1539890687, "GEL-Kayano 25 OBI s", 0, "Black"],
            5bb4c21e2d49b22230670aec:[1539877225, "Nike SB Check Solar Leather Skate Shoes", 0,"White"], 
            5bb4c21e2d49b2223067455e:[1539890979, "GEL-Quantum 360 4", 0, "Grey"],
            5bb4c21e2d49b22230672934:[1539890693, "GEL-Contend 4 Wide Width s", 0, "Blue"],
            5bb4c21f2d49b222306766b9:[1539890671, "GEL-DS Trainer 23 Casual Shoes", 0, "White"],
            5bb7aadfda02b1355e3c0cdb:[1539890596, "Womens ASICS GT-2000 V6", 0, "Black"]
}

How can i sort this object based on timestamp i.e the first value in array[4]?

nash63
  • 59
  • 1
  • 12
  • 6
    Your `myArray` is not an array. – Md Johirul Islam Oct 19 '18 at 15:14
  • Possible duplicate of [sort outer array based on values in inner array, javascript](https://stackoverflow.com/questions/2793847/sort-outer-array-based-on-values-in-inner-array-javascript) – Pim Oct 19 '18 at 15:14
  • 1
    Keys in an object are not guarenteed to have any order. If you want to guarentee order, and sort, use a real array – Taplar Oct 19 '18 at 15:18

1 Answers1

0

I have made some change to your representation. Check whether the following solves your problem.

var myArray = [
            {"5bb4c21d2d49b2223066cf62":[1539890687, "GEL-Kayano 25 OBI s", 0, "Black"]},
            {"5bb4c21e2d49b22230670aec":[1539877225, "Nike SB Check Solar Leather Skate Shoes", 0,"White"]}, 
            {"5bb4c21e2d49b2223067455e":[1539890979, "GEL-Quantum 360 4", 0, "Grey"]},
            {"5bb4c21e2d49b22230672934":[1539890693, "GEL-Contend 4 Wide Width s", 0, "Blue"]},
            {"5bb4c21f2d49b222306766b9":[1539890671, "GEL-DS Trainer 23 Casual Shoes", 0, "White"]},
            {"5bb7aadfda02b1355e3c0cdb":[1539890596, "Womens ASICS GT-2000 V6", 0, "Black"]}
];
myArray.sort((a,b) =>{
  return Object.values(a)[0][0] - Object.values(b)[0][0];
});
console.log(myArray);
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56