-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]?

3
  • 6
    Your myArray is not an array. Commented Oct 19, 2018 at 15:14
  • Possible duplicate of sort outer array based on values in inner array, javascript Commented Oct 19, 2018 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 Commented Oct 19, 2018 at 15:18

1 Answer 1

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

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

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.