Sort by id in AngularJS (Javascript)

1

I have an AngularJS object:

$scope.listaDoCarrinho = [0: {
        id: "55",
        setor: "alimento",
        foto: "Produtos/Produto (55).jpg",
        descr: "Espaguete Renata",
        de: 15,
        …
    }
    1: {
        id: "1000",
        setor: "biscoitos",
        foto: "Produtos/Produto (1000).jpg",
        descr: "Biscoito Pit-Stop",
        de: 3,
        …
    }
    2: {
        id: "3",
        setor: "higiene",
        foto: "Produtos/Produto (3).jpg",
        descr: "Bronzeador 200ml",
        de: 15,
        …
    }
];

I need to change this sequence to id (3 -> 55 -> 1000). Is there a way to do it without it being at the time of ng-repeat?

Note: "-" (dash) in the code is just to inform you that the array has more insignificant properties.

    
asked by anonymous 12.10.2017 / 05:55

1 answer

1

You can use $filter orderBy :

$scope.listaDoCarrinho = $filter('orderBy')($scope.listaDoCarrinho, 'id');

But you see, its id attribute is String , so it will be sorted alphabetically.

  

orderBy

     Returns an array containing the items from the specified collection, ordered by a comparator function based on the values computed using the predicate expression.

Free translation:

  

Returns an array containing the items of a specified collection, sorted by a comparison function based on the values computed using the predicate expression.

    
12.10.2017 / 06:06