'Javascript - grouping an array and then sorting the grouped result based on length

this is the array ;

var dataArr = [
  {
    "permalink": /* link*/
      "subreddit": "mac"
  }, {
    "permalink": /* link*/
      "subreddit": "worldnews"
  }, {
    "permalink": /* link*/
      "subreddit": "MushroomGrowers"
  }, {
    "permalink": /* link*/
      "subreddit": "chrome"
  }, {
    "permalink": /* link*/
      "subreddit": "onions"
  }, {
    "permalink": /* link*/
      "subreddit": "onions"
  }, {
    "permalink": /* link*/
      "subreddit": "SquaredCircle"
  }.....
]

grouping seems simple enough based on the "subreddit" key using underscore

const grouped = _.groupBy( dataArr, 'subreddit' )

returns an object something like this

{
  ArtisanVideos: [
    {
      "permalink": ' link ',
      subreddit: "ArtisanVideos"
    },
    {
      "permalink": ' link ',
      subreddit: "ArtisanVideos"
    },
    {
      "permalink": ' link ',
      subreddit: "ArtisanVideos"
    }
  ],
  chrome: [
    {
      "permalink": ' link ',
      subreddit: "chrome"
    }
  ],
  laravel: [
    {
      "permalink": ' link ',
      subreddit: "laravel"
    },
    {
      "permalink": ' link ',
      subreddit: "laravel"
    },
    {
      "permalink": ' link ',
      subreddit: "laravel"
    }
  ],
  mac: [
    {
      "permalink": ' link ',
      subreddit: "mac"
    }
  ]
}

Now, how do I sort the grouped objects based on the length of the array



Solution 1:[1]

Ok. Here you have it.

First, we sort the data:

    let sorted = data.sort((a, b) => (a.subreddit > b.subreddit) ? 1 : -1);
    console.log("sorted: ", sorted);

Please notice that we only sort by the property subreddit just to group them together for the next step.

Next, we create a new object, by creating a new array if the property is undefined:

  let output = {};
    data.forEach((elem) => {
      if(output[elem.subreddit] === undefined){
        output[elem.subreddit] = new Array();
      }
      output[elem.subreddit].push(elem);
    });

Finally, we do a test to make sure everything is as we expected:

console.log("same? :", JSON.stringify(expected) == JSON.stringify(output));

let data = [
  { "permalink" : "https://www.apple.com", "subreddit": "mac" }, 
  { "permalink" : "https://www.microsoft.com", "subreddit": "microsoft" }, 
  { "permalink" : "https://www.xcode.com", "subreddit": "mac" }, 
  { "permalink" : "https://www.xbox.com", "subreddit": "microsoft" }, 
];

let expected = {
  "mac": [
    { "permalink" : "https://www.apple.com", "subreddit": "mac" }, 
    { "permalink" : "https://www.xcode.com", "subreddit": "mac" }
  ],
  "microsoft": [
    { "permalink" : "https://www.microsoft.com", "subreddit": "microsoft" }, 
    { "permalink" : "https://www.xbox.com", "subreddit": "microsoft" }, 
  ]
};


let sorted = data.sort((a, b) => (a.subreddit > b.subreddit) ? 1 : -1);
console.log("sorted: ", sorted);

let output = {};
data.forEach((elem) => {
  if(output[elem.subreddit] === undefined){
    output[elem.subreddit] = new Array();
  }
  output[elem.subreddit].push(elem);
});

console.log("output: ", output);
console.log("same? :", JSON.stringify(expected) == JSON.stringify(output));

Enjoy.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 acarlstein