5

I have the following demo data.

var demoData= [{"EntryGroupDate":"November 2013", 
                "DisplayName": "Hans Meier (November)", 
                "EntryGroupDateSort": 11},
               {"EntryGroupDate":"August 2013", 
                "DisplayName": "Franz Mueller (August)", 
                "EntryGroupDateSort": 8},
               {"EntryGroupDate":"November 2013", 
                "DisplayName": "Franz Huber (November)", 
                "EntryGroupDateSort": 11},
               {"EntryGroupDate":"Juli 2013", 
                "DisplayName": "Franz Schmidt (Juli)", 
                "EntryGroupDateSort": 7}
              ];

What would be the best way to group them first by EntryGroupDateSort and sort them afterwards by the same criteria. For the output I need all the information of the original array.

I have just fiddled around with UnderscoreJS but did not get the desired result by using the following code.

var myPersons = demoData;
var groups = _.groupBy(myPersons, "EntryGroupDate");
console.log(groups);
groups = _(groups).sortBy(function (item) {
    return item.EntryGroupDateSort;
});
console.log(groups);

The first console output shows the data in the format I would like to have after sorting the data.

Hopefully someone can point me in the right direction.

7
  • 1
    What exactly do you mean by "group"? I mean, what is the expected output? Commented Dec 2, 2013 at 14:03
  • how do you intend to use this? We can't see what you can see in console, so stating it's what you want doesn't help Commented Dec 2, 2013 at 14:04
  • Sorting when you expect the same item to appear does group. This makes it seem like you want to sort by two things at once (sort by EntryGroupDate, if that was 0, sort by blah) Commented Dec 2, 2013 at 14:04
  • I just want to create an overview of new employees. Therefore I need a grouping by the month of entry and afterwards I would like to sort the data the month entry. I hope this makes my intended use clearer. Commented Dec 2, 2013 at 14:07
  • Group how? Once sorted, they will appear in the array with the lowest month first etc ? Commented Dec 2, 2013 at 14:08

3 Answers 3

5

You can do the sort across the entire array first and then group by:

JSFIDDLE

var demoData= [{"EntryGroupDate":"November 2013", 
                "DisplayName": "Hans Meier (November)", 
                "EntryGroupDateSort": 11},
               {"EntryGroupDate":"August 2013", 
                "DisplayName": "Franz Mueller (August)", 
                "EntryGroupDateSort": 8},
               {"EntryGroupDate":"November 2013", 
                "DisplayName": "Franz Huber (November)", 
                "EntryGroupDateSort": 11},
               {"EntryGroupDate":"Juli 2013", 
                "DisplayName": "Franz Schmidt (Juli)", 
                "EntryGroupDateSort": 7}
              ];


_.sortBy( demoData, function(x){ return x.EntryGroupDateSort; } );
groups = _.groupBy( demoData, 'EntryGroupDate' );
console.log( groups );

If you want to sort after grouping then you can use (sorting on DisplayName this time):

JSFIDDLE

groups = _.groupBy( demoData, 'EntryGroupDate' );
for ( var key in groups )
    groups[key].sort( function(a,b){ var x = a.DisplayName, y = b.DisplayName; return x === y ? 0 : x < y ? -1 : 1; } );
console.log( groups );
Sign up to request clarification or add additional context in comments.

3 Comments

The keys/values of an object don't have guaranteed order. The output I get from your answer isn't in any order: [Object] ▶︎ "August 2013": Array[1], "Juli 2013": Array[1], "November 2013": Array[2].
It is as the OP asked for: "The first console output shows the data in the format I would like to have after sorting the data."
OP was asking for the grouped object's keys to be sorted by EntryGroupDateSort which is not possible as an object's keys have no order. Neither of your answers produce the output OP was looking for. Your first answer was especially bad because you wasted CPU time to sort the initial array even though it makes no difference after you call _.groupBy().
1

The _.groupBy() function returns an object and you cannot "sort" an object. The keys/values of an object do not have any order. The _.sortBy() method transforms your object (containing only arrays) into an array of arrays and sorts the values based on EntryGroupDateSort. The problem is that you're trying to sort the array of arrays by the EntryGroupDateSort value (which isn't a property of an array). What you want to do is sort the grouped object of arrays by the _.first() item in each array. Here's the closest you're going to get to what you want:

var groups = _(demoData).chain()
    .groupBy("EntryGroupDate")
    .tap(function(data){ console.log(data) })
    .sortBy(function(data){
      // Note that we're sorting by EntryGroupDateSort
      // from the _.first() item in the array
      return _.first(data).EntryGroupDateSort
    })
    .tap(function(data){ console.log(data) })
    .value();

Comments

0

Try this,

 demoData.sort(function(a, b) {
         var textA = a.EntryGroupDateSort;
         var textB = b.EntryGroupDateSort;

         return (textA < textB) ? 1 : (textA > textB) ? -1 : 0;
         //return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; //for ascending.

  });

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.