angular-filter icon indicating copy to clipboard operation
angular-filter copied to clipboard

Grouping reverses order

Open GoceBogdanovski opened this issue 9 years ago • 6 comments

Hello i notice that if i use groupBy function from Your filter, it reverses the original order of the initial array.

Can you tell me if it is possible to use something like groupBy: 'ID' and then reverse?

GoceBogdanovski avatar Feb 15 '16 09:02 GoceBogdanovski

angular.module('a8m.group-by', ['a8m.filter-watcher']) .filter('groupBy', ['$parse', 'filterWatcher', function ($parse, filterWatcher) { return function (collection, property) {

          if (!isObject(collection) || isUndefined(property)) {
              return collection;
          }

          return filterWatcher.isMemoized('groupBy', arguments) ||
            filterWatcher.memoize('groupBy', arguments, this,
              _groupBy(collection, $parse(property)));

          /**
           * groupBy function
           * @param collection
           * @param getter
           * @returns {{}}
           */
          function _groupBy(collection, getter) {
              var result = {};
              var arrayResult = [];
              var prop;

              forEach(collection, function (elm) {
                  prop = getter(elm);

                  if (!result[prop]) {
                      result[prop] = [];
                  }
                  result[prop].push(elm);
              });

              forEach(result, function (value, index) {
                  arrayResult.push(value);
              });

              return arrayResult.reverse();
          }
      }
  }]);

GoceBogdanovski avatar Apr 15 '16 10:04 GoceBogdanovski

Hi @1cm, do you have an example plunker so I can reproduce the behavior you're seeing?

toddbranch avatar Apr 15 '16 12:04 toddbranch

This one is done and it is working as it should. The problem was, for example, if send some ordered array to the groupBy module, the module will group it as it is should, but it wil revers the original order, pulled from db.

GoceBogdanovski avatar Apr 15 '16 13:04 GoceBogdanovski

I just add this forEach(result, function (value, index) { arrayResult.push(value); });

GoceBogdanovski avatar Apr 15 '16 13:04 GoceBogdanovski

Is there a fix you want to be made? Or should I close because this is working?

toddbranch avatar Apr 15 '16 14:04 toddbranch

If you want you can implement this in your plug-in. I made veraion for me. Thanks for the response. :)

GoceBogdanovski avatar Apr 15 '16 14:04 GoceBogdanovski