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);
jimjamian
  • 93
  • 1
  • 10
  • "videos" is an Object of **Key / Value** system, there is no needs of an order, this is not an Array – Mister Jojo Jan 18 '21 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. – rahulpsd18 Jan 18 '21 at 21:58
  • It *is* possible to sort the object keys (via https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key), 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? – zcoop98 Jan 18 '21 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. – rahulpsd18 Jan 18 '21 at 22:04

2 Answers2

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

david snyder
  • 337
  • 3
  • 16
  • Basically insertion order is not respected since your keys parse as integers. Otherwise it would be. Reference: https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – vincent Jan 19 '21 at 04:51
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
SuperJumbo
  • 519
  • 3
  • 13