angularjssortingangularjs-orderby

Angular multiple sort, order by


probably a very nooby question and I'm missing obvious but I'm using a function to sort my results. Each result has a price and a weighting (priority basically)

I want the results ordered by price but there are results with the same price, they need to be ordered by the weight. I can also reverse the ordering, so low-high/high-low.

Here is a plunker with some noddy data:

http://plnkr.co/edit/VH2WvyJMsLSTpWJawT2f?p=preview

In my app (I have hundreds of results), I used this

$scope.orderByFunction = function (result) {
    if ($scope.orderBy == 'price-low-high') {
        return result.totalPrice.amount + result.boost;
    }
    if ($scope.orderBy == 'price-high-low') {
        return -result.totalPrice.amount + result.boost;
    }
    else return result.totalPrice.amount + result.boost;
};

Whilst I thought it was initially successful, it got some wrong as it show higher priced results above lower priced ones. Appreciate it's hard for me to show that so I figured I'd ask the noddy question first :)


Solution

  • Answered over at https://groups.google.com/forum/#!topic/angular/XAJ2hyeRSTU

    This is the html http://plnkr.co/edit/VH2WvyJMsLSTpWJawT2f?p=preview orderBy: [orderByPrice, orderByPriority]

    and the JS

    $scope.orderByPrice = function (result) {
        if ($scope.orderBy == 'price-low-high') {
            return result.amount;
        }
        if ($scope.orderBy == 'price-high-low') {
            return -result.amount;
        }
        else return result.totalPrice.amount;
    };
    $scope.orderByPriority = function (result) {
            return result.priority;
    };