angular-filter
angular-filter copied to clipboard
Grouping reverses order
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?
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();
}
}
}]);
Hi @1cm, do you have an example plunker so I can reproduce the behavior you're seeing?
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.
I just add this forEach(result, function (value, index) { arrayResult.push(value); });
Is there a fix you want to be made? Or should I close because this is working?
If you want you can implement this in your plug-in. I made veraion for me. Thanks for the response. :)