Sort JSON response by key value

后端 未结 3 2097
半阙折子戏
半阙折子戏 2021-01-20 16:57

Before you tag this as duplicate - I\'ve gone through these answers:

Sort JSON by array key value

Sort a JSON array object using Javascript by value

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-20 17:41

    This is my approach, just convert to an array and then get back the JSON Object.

    UPDATE: What I did was create an array of every page then using the Array.prototype.sort() function for Arrays I compared the index for each page then using a for-loop I recreated the JSON object using the objects within the sorted Array.

    var json = {
      "pages": {
        "983": {
          "pageid": 983,
          "ns": 0,
          "title": "Albert Camus",
          "index": 10,
          "contentmodel": "wikitext",
          "pagelanguage": "en",
          "pagelanguagehtmlcode": "en",
          "pagelanguagedir": "ltr",
          "touched": "2018-01-26T09:34:35Z",
          "lastrevid": 822358239,
          "length": 53639,
          "fullurl": "https://en.wikipedia.org/wiki/Albert_Camus",
          "editurl": "https://en.wikipedia.org/w/index.php?title=Albert_Camus&action=edit",
          "canonicalurl": "https://en.wikipedia.org/wiki/Albert_Camus",
          "thumbnail": {
            "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Albert_Camus%2C_gagnant_de_prix_Nobel%2C_portrait_en_buste%2C_pos%C3%A9_au_bureau%2C_faisant_face_%C3%A0_gauche%2C_cigarette_de_tabagisme.jpg/42px-Albert_Camus%2C_gagnant_de_prix_Nobel%2C_portrait_en_buste%2C_pos%C3%A9_au_bureau%2C_faisant_face_%C3%A0_gauche%2C_cigarette_de_tabagisme.jpg",
            "width": 42,
            "height": 50
          },
          "pageimage": "Albert_Camus,_gagnant_de_prix_Nobel,_portrait_en_buste,_posé_au_bureau,_faisant_face_à_gauche,_cigarette_de_tabagisme.jpg"
        },
        "736": {
          "pageid": 736,
          "ns": 0,
          "title": "Albert Einstein",
          "index": 2,
          "contentmodel": "wikitext",
          "pagelanguage": "en",
          "pagelanguagehtmlcode": "en",
          "pagelanguagedir": "ltr",
          "touched": "2018-01-24T22:40:11Z",
          "lastrevid": 821432412,
          "length": 145560,
          "fullurl": "https://en.wikipedia.org/wiki/Albert_Einstein",
          "editurl": "https://en.wikipedia.org/w/index.php?title=Albert_Einstein&action=edit",
          "canonicalurl": "https://en.wikipedia.org/wiki/Albert_Einstein",
          "thumbnail": {
            "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Einstein_1921_by_F_Schmutzer_-_restoration.jpg/38px-Einstein_1921_by_F_Schmutzer_-_restoration.jpg",
            "width": 38,
            "height": 50
          },
          "pageimage": "Einstein_1921_by_F_Schmutzer_-_restoration.jpg"
        }
      }
    };
    
    var array = [];
    for (key in json.pages) {
      array.push(json.pages[key]);
    }
    
    array.sort(function(a, b) {
      return a.index - b.index;
    });
    
    json = {
      "pages": {}
    };
    
    for (var i = 0; i < array.length; i++) {
      json.pages[array[i]['pageid']] = array[i];
    }
    
    console.log(json);

    Hope it helps!

提交回复
热议问题