0

I have a React.js application that is being fed JSON data from the backend.

I'm unsure of how to sort the objects by a key that is a string rather than number - if it was the latter, I could've used numerous examples.

Here's a sample string. The object I want to sort for is High, Medium and Low. I want to sort with high first, then medium, and then low ones.

{"Medium":{"This is inbetween!":[{"key":"123", "type":"inbetween"}]},
"High":{"This is the highest!":[{"key":"3333", "type":"highest"}]},
"Medium":{"This is inbetween again":[{"key":"12333123", "type":"power"}]},
"Low":{"This is the lowest!":[{"key":"123465", "type":"super low"}]}}

How do I sort this JSON data into strings high, medium and low in an array?

7
  • 3
    Why don't you update the question to show what you'd like the result array to look like? The input object is not an array and can't be sorted. Commented Feb 28, 2017 at 19:46
  • Well, it can be sorted to an array .. anyway, there is only one "Medium" item given the object input. Commented Feb 28, 2017 at 19:48
  • 1
    You can certainly do this if you want the result as an array (see Pointy's comment). What have you tried? Where are you stuck? What has your research turned up? Commented Feb 28, 2017 at 19:48
  • 1
    That JSON is both invalid (missing comma at least, possibly more) and, separately, probably not what you intend it to be (odd nesting). Commented Feb 28, 2017 at 19:48
  • @T.J.Crowder was trying to make an example of my input data, might've made a typo somewhere. Commented Feb 28, 2017 at 19:51

1 Answer 1

2

So as your "object" is most probably an array (it would be an invalid object, with duplicate declarations, e.g. of the Medium property) I assume that the following will be how your data is...

You can sort that array of objects by creating another "order" object, assigning an order number to the keys.

I think the following snippet will do what you want:

let arr = [
    {
        "Medium": {
            "This is inbetween!": [
                {
                    "key": "123",
                    "type": "inbetween"
                }
            ]
        }
    },
    {
        "High": {
            "This is the highest!": [
                {
                    "key": "3333",
                    "type": "highest"
                }
            ]
        }
    },
    {
        "Medium": {
            "This is inbetween again": [
                {
                    "key": "12333123",
                    "type": "power"
                }
            ]
        }
    },
    {
        "Low": {
            "This is the lowest!": [
                {
                    "key": "123465",
                    "type": "super low"
                }
            ]
        }
    }
];

let order = {
    High: -3,
    Medium: -2,
    Low: -1
}


arr = arr.sort((a,b) => {
    return order[Object.keys(a)[0]] - order[Object.keys(b)[0]];
});

console.log(arr);

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

4 Comments

I tried your example on some sample data(That has many high and medium ones) and I only an array with len 3 where the content was only medium ones. Thanks for the example though, I'll work out from it!
while that is a nice idea (and a dupe in this case), i suggest to use not values with zero in the order object, becaus that does not create a space for handling unknown values and does not move as (possibly) wanted to start or end of the array.
With your method, how would I access, say, all objects where the key is "high"? console.log(Arr[].High) doesn't seem to work, and it doesn't sound right either(I'm a tad tired :))
You'd need to filter the array. to get all entries with high, this should work: console.log(arr.filter(v => Object.keys(v)[0] === 'High')); @cbll

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.