JavaScript Best way to group and sort an array of objects

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.

Answer:1

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 );
Answer:2

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.

  });
Answer:3

I am trying to make a web form which has the option to add and remove a new element. I can get this working as buttons outside of the source or cloned elements, but I am having a problem getting a '...

I am trying to make a web form which has the option to add and remove a new element. I can get this working as buttons outside of the source or cloned elements, but I am having a problem getting a '...

  1. jquery remove cloned element
  2. jquery remove cloned row
  3. jquery remove cloned object
  4. jquery remove clone
  5. jquery remove clone div
  6. jquery remove clone append
  7. jquery remove clone table
  8. jquery remove clone on click
  9. jquery clone remove class
  10. jquery clone remove id
  11. jquery clone remove attribute
  12. jquery clone remove events
  13. jquery clone remove values
  14. jquery clone remove original
  15. jquery clone remove data

I've been experimenting and it seems like only form elements and the window object can trigger the keypress event. I checked MDN documentation, and also the spec, but couldn't find anywhere that ...

I've been experimenting and it seems like only form elements and the window object can trigger the keypress event. I checked MDN documentation, and also the spec, but couldn't find anywhere that ...

When defining a class in Javascript, how can I call one method from another one? exports.myClass = function () { this.init = function() { myInternalMethod(); } this....

When defining a class in Javascript, how can I call one method from another one? exports.myClass = function () { this.init = function() { myInternalMethod(); } this....

  1. calling method from another class
  2. calling method from another class c#
  3. calling method from another class python
  4. calling method from different class java
  5. calling method from main java
  6. calling method from parent class python
  7. calling method from same class java
  8. calling method from superclass java
  9. calling method from class python
  10. calling method from constructor java
  11. calling method from abstract class java
  12. calling method from parent class java
  13. calling method from another component angular
  14. calling method from this
  15. call method from another activity android
  16. call method from another class react
  17. call method from another script unity
  18. call method from activity to fragment
  19. call method from another controller laravel
  20. call method from another component vue

I'm wanting to use jQuery to dynamically modify the background color of a <div>, based on its CSS property, width value. The usage is for some form of color-coded meter, which indicates how ...

I'm wanting to use jQuery to dynamically modify the background color of a <div>, based on its CSS property, width value. The usage is for some form of color-coded meter, which indicates how ...

  1. change background color in photoshop
  2. change background color google docs
  3. change background color html
  4. change background color instagram story
  5. change background color illustrator
  6. change background color in word
  7. change background color css
  8. change background color indesign
  9. change background color on iphone
  10. change background color javascript
  11. change background color in outlook
  12. change background color using css mdn
  13. change background color online
  14. change background color after effects
  15. change background color of picture
  16. change background color excel
  17. change background color gimp
  18. change background color premiere
  19. change background color instagram story video
  20. change background color in pages