1

I am trying to sort through some JSON by highest amount to lowest with some nested values. Take the following object:

"videos": {
    "0": {
      "video_name": "Calls",
      "video_filename": "ALEXA_CALLS.mp4",
      "count": 1
    },
    "1": {
      "video_name": "Routines",
      "video_filename": "ROUTINES.mp4",
      "count": 3
    },
    "4": {
      "video_name": "Photos",
      "video_filename": "PHOTOS.mp4",
      "count": 4
    }
  }

Is it possible to sort through this based on the 'count value', so the Photos would be the first property, then Routines, etc. Sort of like this:

"videos": {       
    "4": {
      "video_name": "Photos",
      "video_filename": "PHOTOS.mp4",
      "count": 4
    },
    "1": {
      "video_name": "Routines",
      "video_filename": "ROUTINES.mp4",
      "count": 3
    },
    "0": {
      "video_name": "Calls",
      "video_filename": "ALEXA_CALLS.mp4",
      "count": 1
    },
  }

I've seen from other questions that you can sort if you convert the json object into an array and do something with .sort, but it seems the numbers "0", "1", "4", etc can cause issues as I can't figure out how to generically reference the whole object. With other stuff I am doing, I need to do stuff like videos[0].video_name.

Anyone seen something like this before? Any tips would be great !

Update with example:

let v = {
"0": {
  "video_name": "Calls",
  "video_filename": "ALEXA_CALLS.mp4",
  "count": 1
},
"1": {
  "video_name": "Routines",
  "video_filename": "ROUTINES.mp4",
  "count": 3
},
"4": {
  "video_name": "Photos",
  "video_filename": "PHOTOS.mp4",
  "count": 4
}
};

v.sort(function(a, b) {
return a.count > b.count;
});

console.log(v);
4
  • "videos" is an Object of Key / Value system, there is no needs of an order, this is not an Array Commented Jan 18, 2021 at 21:56
  • If you need it sorted based on count then you need order => you need Array. So first try converting the object to an array and then write the sorting logic. Commented Jan 18, 2021 at 21:58
  • It is possible to sort the object keys (via stackoverflow.com/questions/5467129/…), but since this isn't an array, sorting isn't super relevant. You shouldn't necessarily have any issues referencing the whole objects if you convert to an array... but if you are, can you detail those specific issues you reference? Commented Jan 18, 2021 at 21:58
  • Yes, although it is possible but JavaScript doesn't guarantee object property order. So IMO, doing so is probably a bad idea. Commented Jan 18, 2021 at 22:04

2 Answers 2

3

You can do it by converting it to an array

const obj = {
  "videos": {
    "0": {
      "video_name": "Calls",
      "video_filename": "ALEXA_CALLS.mp4",
      "count": 1
    },
    "1": {
      "video_name": "Routines",
      "video_filename": "ROUTINES.mp4",
      "count": 3
    },
    "4": {
      "video_name": "Photos",
      "video_filename": "PHOTOS.mp4",
      "count": 4
    }
  }
}

const videos = obj.videos

const videoArray = Object.entries(videos).map(([key, value]) => ({...value, key: key}))
const sortedVideoArray = videoArray.sort((a, b) => b.count - a.count)

console.log(sortedVideoArray)

there is no point in putting the values from the array back into an object since js objects do not guarantee that they will maintain insert order but that code snippet above does allow you to maintain which video had which key in the object

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

1 Comment

Basically insertion order is not respected since your keys parse as integers. Otherwise it would be. Reference: stackoverflow.com/questions/5525795/…
1

First your data isn't quite JSON; it should be wrapped in another set of brackets like

   {
    "videos": {       
        "4": {
          "video_name": "Photos",
          "video_filename": "PHOTOS.mp4",
          "count": 4
        },
        "1": {
          "video_name": "Routines",
          "video_filename": "ROUTINES.mp4",
          "count": 3
        },
        "0": {
          "video_name": "Calls",
          "video_filename": "ALEXA_CALLS.mp4",
          "count": 1
        }
      }
    }

From there you can parse it, grab out the values and sort via

const data = JSON.parse(...)

Object.values(data.videos).sort((a,b) => a.count - b.count)

Also, you can reverse the sort by reversing the body of the sort function

(a,b) => b.count - a.count

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.