-4

hi i need sort this object by catId :

book {
books : {
1: {id: 1, name: "javascript", catId: 2}
2: {id: 2, name: "ayat", catId: 1}
3: {id: 3, name: "olom", catId: 3}
4: {id: 4, name: "css", catId: 2}
5: {id: 5, name: "chap", catId: 1}
}
}

output :

book {
books : {
2: {id: 2, name: "ayat", catId: 1}
5: {id: 5, name: "chap", catId: 1}
4: {id: 4, name: "css", catId: 2}
1: {id: 1, name: "javascript", catId: 2}
3: {id: 3, name: "olom", catId: 3}
}
}

or for example push {id: 3, name: "olom", catId: 3} to a empty array :

arrBooks [
{id: 2, name: "ayat", catId: 1}
{id: 5, name: "chap", catId: 1}
{id: 4, name: "css", catId: 2}
{id: 1, name: "javascript", catId: 2}
{id: 3, name: "olom", catId: 3}
]
  • Is `books` an object or array? If it's an object, this is not possible. If it's an array, there are hundreds of duplicates – adiga Apr 14 '19 at 10:22
  • 1
    What you want as output `object` or `array` ?if `object` than you no you can't and if `array` than yes you can use `sort` – Code Maniac Apr 14 '19 at 10:23
  • 1
    @Klimenkomud it's not possible since the keys are integers https://stackoverflow.com/a/38218582/3082296 – adiga Apr 14 '19 at 10:32
  • @CodeManiac i want output array like this : `arrBooks [ {id: 2, name: "ayat", catId: 1} {id: 5, name: "chap", catId: 1} {id: 4, name: "css", catId: 2} {id: 1, name: "javascript", catId: 2} {id: 3, name: "olom", catId: 3} ]` from top object – Mohammad Akbari Apr 14 '19 at 10:33
  • @MohammadAkbari you can get values from `Object.values(book.book)` if (bools.book is an object ) and than use [sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) – Code Maniac Apr 14 '19 at 10:36
  • @Klimenkomud no you can't, you can read [this](https://www.stefanjudis.com/today-i-learned/property-order-is-predictable-in-javascript-objects-since-es2015/) – Code Maniac Apr 14 '19 at 10:38
  • @adiga book.books is object but i want output value of this object into array like this : `arrBooks [ {id: 2, name: "ayat", catId: 1} {id: 5, name: "chap", catId: 1} {id: 4, name: "css", catId: 2} {id: 1, name: "javascript", catId: 2} {id: 3, name: "olom", catId: 3} ]` – Mohammad Akbari Apr 14 '19 at 10:39
  • 1
    @CodeManiac yes Exactly i want this Thankful – Mohammad Akbari Apr 14 '19 at 10:44

1 Answers1

3

You can use Object.values and sort like this:

const book = {
books : {
   1: {id: 1, name: "javascript", catId: 2},
   2: {id: 2, name: "ayat", catId: 1},
   3: {id: 3, name: "olom", catId: 3},
   4: {id: 4, name: "css", catId: 2},
   5: {id: 5, name: "chap", catId: 1}
 }
}

const sortedBooks = Object.values(book.books)
                          .sort((a, b) => a.catId - b.catId)
                          
console.log(sortedBooks)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
adiga
  • 34,372
  • 9
  • 61
  • 83
  • How can you start a wiki answer? – f-CJ Apr 14 '19 at 10:46
  • @f-CJ while posting answer you see a checkbox on `bottom right` stating `community wiki` mark that if you want to add answer as wiki, you can see [here](https://stackoverflow.com/help/privileges/community-wiki) – Code Maniac Apr 14 '19 at 10:48