1

I'm trying to sort an array of javascript objects by their properties for an angularjs application. I need to be able to sort by Numbers and Strings.

So far i have expanded the filter from Armin provided in this thread to sort numbers and strings (see code below).

What i am still missing is the ability to give a nested property (like user.surname) to the filter. It should be dynamic, so that i would be able to provide any depth of nested properties to it. Is there a way to do this?

Here's my filter code:

angular.module('app')
    .filter('orderObjectBy', function(){
        return function(input, filterBy) {
            if (!angular.isObject(input)) return input;

            var attribute = filterBy;
            var reverse = false;

            if (filterBy.substr(0,1) == '-') {
                attribute = filterBy.substr(1);
                reverse = true;
            }

            var array = [];
            for(var objectKey in input) {
                array.push(input[objectKey]);
            }

            if (parseInt(array[0][attribute])) {
                array.sort(function(a, b){
                    a = parseInt(a[attribute]);
                    b = parseInt(b[attribute]);
                    return a - b;
                });
            } else {
                array.sort(function (a,b) {
                    if (a[attribute] < b[attribute]) {
                        return -1;
                    } else if (a[attribute] > b[attribute]) {
                        return 1;
                    } else {
                        return 0;
                    }
                });
            }

            if (reverse) {
                return array.reverse();
            } else {
                return array;
            }

        }
    });
5
  • Can you provide jsfiddle or plunker? Commented Mar 11, 2014 at 7:51
  • see using filters effects the performance and if you are sorting on 100+ i.e. (nos of rows* nos of columns) data elements then i don't think you should approach it using filters. Rather write a directive which would re position objects in your scope object which you use in ng-repeat Commented Mar 11, 2014 at 8:11
  • From the documentation: docs.angularjs.org/api/ng/filter/orderBy There is an example of usage of the orderBy filter on an array of objects, and the filtering is done on a property. How's that different from your problem ? The thread you mention is talking about sorting properties of an object ... not an array of objects. Commented Mar 11, 2014 at 8:19
  • @RishulMatta: I see your point... I don't think that the lists in my app will exceed 100+ items, at least none except one list. So i think i will use the backend to sort the list for me because it is an inhouse app, so bandwith and speed are not the problem. Nevertheless your idea using a directive sounds good, i will test this as well. Commented Mar 13, 2014 at 7:56
  • @TimothéeJeannin: ah yes... i had an object of objects first but then changed it myself to an array. So now it is an array of objects. Haven't thought about the it again... organisation blindness i think... So but still there is the problem with the nested properties. How to iterate about a dynamic depth of nested properties to sort the objects inside the array after them? Commented Mar 13, 2014 at 8:01

1 Answer 1

1

its pretty straight forward check this out: http://jsbin.com/duzurazo/2/edit

you can use something like

  <li ng-repeat="prop in props | orderBy:'order.v'"> {{prop.order.v}} </li>

where model is something like

  $scope.props = [{order:{v:"1"}},{order:{v:"5"}},{order:{v:"2"}}];
Sign up to request clarification or add additional context in comments.

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.