0

Would someone please tell me how I can sort this kind of JSON Array by the "name" in ascending alphabetical order?

{
    "response": {
        "game_count": 86,
        "games": [
            "10": {
                "appid": 10,
                "name": "Counter-Strike",
                "playtime_forever": 7604,
                "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5",
                "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f",
                "has_community_visible_stats": true
            },
            "80": {
                "appid": 80,
                "name": "Counter-Strike: Condition Zero",
                "playtime_forever": 62,
                "img_icon_url": "077b050ef3e89cd84e2c5a575d78d53b54058236",
                "img_logo_url": "acdb28ba1b4c2fcc526332c1b63fc0f7533f087f"
            },
                .
                .
                .
            ]
     }
}
TofferJ
  • 4,216
  • 1
  • 31
  • 43
  • I'll use `lodash` and the `_.sortBy` function. – Hackerman Jul 25 '17 at 16:27
  • @Hackerman lodash in a PHP file? – BeetleJuice Jul 25 '17 at 16:30
  • Oooh, you want to sort it with php....so then you should use `json_decode` and start sorting out the games array by name...I'll let you my client side solution, just in case https://jsfiddle.net/d18s7ndk/ – Hackerman Jul 25 '17 at 16:32
  • @Hackerman not me. The OP I inferred PHP from the tags on the question. – BeetleJuice Jul 25 '17 at 16:35
  • you want to `json_decode` it as @Hackerman said. Then you won't have a multidimensional array but "games" is an array of obejcts. Have a look here: https://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields – lynx Jul 25 '17 at 16:37

3 Answers3

1

you get required result by this way .

var json_array = {

    "response": {
        "game_count": 86,
        "games": [
            {
                "appid": 10,
                "name": "Counter-Strike",
                "playtime_forever": 7604,
                "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5",
                "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f",
                "has_community_visible_stats": true
            },
            {
                "appid": 80,
                "name": "Counter-Strike: Condition Zero",
                "playtime_forever": 62,
                "img_icon_url": "077b050ef3e89cd84e2c5a575d78d53b54058236",
                "img_logo_url": "acdb28ba1b4c2fcc526332c1b63fc0f7533f087f"
            },
            .
            .
            .
       ]

  }
}

json_array.response.games.sort(sort_by('name', false, function(a){
    return a.toUpperCase()
}));

var sort_by = function(a, m, n){

var key = n ? 
   function(p) {return n(p[a])} : 
   function(q) {return q[a]};

m = !m ? 1 : -1;

return function (x, y) {
   return x = key(x), y = key(y), m * ((x > y) - (y > x));
 } 
}
vjy tiwari
  • 644
  • 1
  • 4
  • 16
1

Your question doesn't make it clear which language your code is in. I assumed PHP because of the PHP tag on the question.

My approach ($json is the raw JSON):

// decode JSON into an array
$arr = json_decode($json,true);

// usort allows sorting by custom attribute
usort(
    $arr['response']['games'],
    function($a,$b){ return $a['name'] < $b['name'] ? -1 : 1;}
    );

// print and check order
print_r($arr);

// turn it back into JSON
$json = json_encode($arr);

Live demo

Docs: usort

BeetleJuice
  • 33,709
  • 16
  • 78
  • 137
1

You could do something like this:

<?php 

  $json = '{
        "response": {
            "game_count": 86,
            "games": [
                {
                    "appid": 10,
                    "name": "Counter-Strike",
                    "playtime_forever": 7604,
                    "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5",
                    "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f",
                    "has_community_visible_stats": true
                },
                {
                    "appid": 80,
                    "name": "Counter-Strike: Condition Zero",
                    "playtime_forever": 62,
                    "img_icon_url": "077b050ef3e89cd84e2c5a575d78d53b54058236",
                    "img_logo_url": "acdb28ba1b4c2fcc526332c1b63fc0f7533f087f"
                },
                {
                    "appid": 23,
                    "name": "Art Attack",
                    "playtime_forever": 76604,
                    "img_icon_url": "6b0312cda02f5f777efa2f3318c307ff9acafbb5",
                    "img_logo_url": "af890f848dd606ac2fd4415de3c3f5e7a66fcb9f",
                    "has_community_visible_stats": true
                }
           ]
      }
}';

$arrayConvertedJson = json_decode($json, true);
$games = $arrayConvertedJson['response']['games'];
uasort($games, 'sortArray');

function sortArray($a, $b){
    if($a['name'] < $b['name']){
        return -1;
    }
    if($a['name'] > $b['name']){
        return 1;
    }
    return 0;
}
$arrayConvertedJson['response']['games'] = $games;

?>
Superdrac
  • 1,190
  • 15
  • 27