-2

I tried to sort an object that contains other objects and arrays in it E.g

let object = {
    '2573': {
        results: [
            {
                "rooms": {
                    "price": 1500
                },
            },
            {
                "rooms": {
                    "price": 1700
                },
            }
        ],
    },
    '2574': {
        results: [
            {
                "rooms": {
                    "price": 1800
                },
            },
            {
                "rooms": {
                    "price": 1900
                },
            }
        ],
    },
    '2575': {
        results: [
            {
                "rooms": {
                    "price": 1850
                },
            },
            {
                "rooms": {
                    "price": 1200
                },
            }
        ],
    }
}

I don't really understand how I could sort this object In the first phase, I should clearly access the first key, after accessing the rooms array and then filtering according to the prices found in each rooms object Is this possible?

I tried something like this

object.sort((a, b) => Object.keys(a)[0] - Object.keys(b)[0]);

But I clearly do not reach the necessary objects to be able to do this filtering

So is it possible to sort by the prices of all the rooms in the object?

results ascending sort

let object = {
    '2574': {
    results: [ 
        {
        "rooms": {
            "price": 1200
        },
        {
            "rooms": {
                "price": 1850
            },
        }
        }
    ],
  },
  '2573': {
    results: [ 
        {
            "rooms": {
                "price": 1500
            },
        },
        {
        "rooms": {
            "price": 1700
        },
        }
    ],
  },
  '2574': {
    results: [ 
        {
            "rooms": {
                "price": 1800
            },
        },
        {
        "rooms": {
            "price": 1900
        },
        }
    ],
  }
}
  • Are you trying to filter or sort? And, how would you sort it? By what key or value? – kelsny Nov 04 '22 at 14:04
  • according to the price of each room – DlukikPython Nov 04 '22 at 14:05
  • Can you show what you would like the resulting, sorted object to look like? – Craig Nov 04 '22 at 14:06
  • Please post compilable code without all the `>` syntaxes. Also, it looks like the object has two `2574` keys which is invalid – adiga Nov 04 '22 at 14:07
  • @Craig large objects 2374, 2375 in order according to the price of the rooms. – DlukikPython Nov 04 '22 at 14:10
  • Are you trying to sort *an object* or are you trying to sort *an array*? The former doesn't make sense, and if the latter then in what way are you comparing the values in that array and what isn't working as expected in that comparison? – David Nov 04 '22 at 14:13
  • Your question is a bit unclear. Please specify you want to sort this array ascending or descending? Also, share a demo result what you want to achieve? – Saud Nov 04 '22 at 14:14
  • 1
    Each "large object" contains multiple "price" values, yet you want to sort by price. I would argue your inability to figure out how to sort flows from your inability to accurately describe the result you want. Once you do that, sorting is easy. – Craig Nov 04 '22 at 14:16
  • I let the result for ascending sorting – DlukikPython Nov 04 '22 at 14:18
  • There you go. Now implement that. :-) – Craig Nov 04 '22 at 14:21
  • You can't sort the keys of this object because they are index-like integers keys. The keys of the object will always be enumerated in ascending numerical order. You'd have to use a `Map` or an array of objects if you want to sort this. [Does JavaScript guarantee object property order?](https://stackoverflow.com/questions/5525795) and [Does ES6 introduce a well-defined order of enumeration for object properties?](https://stackoverflow.com/questions/30076219) – adiga Nov 04 '22 at 14:25
  • @DlukikPython can you provide us the object with proper formatiing – Shreyansh Gupta Nov 04 '22 at 14:28

3 Answers3

1

You loop through the keys to get their results array, and then you sort them independently.

let object={2573:{results:[{rooms:{price:1500}},{rooms:{price:1700}}]},2574:{results:[{rooms:{price:1800}},{rooms:{price:1900}}]},2575:{results:[{rooms:{price:1850}},{rooms:{price:1200}}]}};

const sortResults = (obj) => {
  for (key in obj) {
      obj[key].results.sort((a, b) => {
        return a.rooms.price - b.rooms.price;
      });
  }
  
  return obj;
}

console.log( sortResults(object) );
Rickard Elimää
  • 7,107
  • 3
  • 14
  • 30
0
for(let i in a){
    let element = a[i];
    element.results.sort((el1, el2) => el1.rooms.prices - el2.rooms.prices)
}
console.log(a);
  • 1
    `element = element.results.sort()` is not doing anything. Just `element.results.sort()` is enough – adiga Nov 04 '22 at 14:31
0

i would like to give a one liner for this question although you can do it by writing much of a code but that's not good for a programmer

// this is the actual function that will do the job
function sortNestedObject(o) {
        return Object.keys(o).reduce((p,c)=>({...p,[c]:{results:object[c].results.sort((a,b)=>a.rooms.price-b.rooms.price)}}),{})
    }
    const object = {
        "2573": {
            "results": [
                {
                    "rooms": {
                        "price": 1700
                    }
                },
                {
                    "rooms": {
                        "price": 1500
                    }
                }
            ]
        },
        "2574": {
            "results": [
                {
                    "rooms": {
                        "price": 1800
                    }
                },
                {
                    "rooms": {
                        "price": 1900
                    }
                }
            ]
        },
        "2575": {
            "results": [
                {
                    "rooms": {
                        "price": 1850
                    }
                },
                {
                    "rooms": {
                        "price": 1200
                    }
                }
            ]
        }
    }


    const sorted = sortNestedObject(object)
    console.log(sorted)
Shreyansh Gupta
  • 382
  • 1
  • 10
  • Can you explain please this line? .reduce((p,c)=>({...p,[c]: – DlukikPython Nov 05 '22 at 18:27
  • the reduce method takes the empty dictionary(p) as first argument and second one being one of the array element(s) . Then the {...p} is decontructing the dictionary and adding a values as result to the dictionary which is sorted. i know it is difficult to understand but i can also write the full explained code in few lines if you want .. – Shreyansh Gupta Nov 06 '22 at 04:41