prashanth-g
prashanth-g

Reputation: 1233

How to sort object in angular js with custom sort order?

i want to show array of object's properties in a custom sorted way. following is the array

$scope.weekDays = [
            {
                "day" : "TUESDAY",
                "count": 10
            },
            {
                "day" : "MONDAY",
                "count": 20
            },
            {
                "day" : "WEDNESDAY",
                "count": 30
            },
            {
                "day" : "SUNDAY",
                "count": 60
            }];

if we print day from weekDays it is like TUESDAY, MONDAY, WEDNESDAY, SUNDAY

but i want to show in the order "SUNDAY", "MONDAY", "FRIDAY", "TUESDAY","WEDNESDAY"

for this i did the following

        $scope.orde = ["SUNDAY", "MONDAY", "FRIDAY", "TUESDAY","WEDNESDAY"];

        $scope.Sorted = [];

        $scope.SortCustomOrder = function() {
            var _c =0;
            for(var i = 0; i < $scope.orde.length; i++) {
                for (var _i = 0; _i < $scope.weekDays.length; _i++) {
                    if($scope.weekDays[_i].day==$scope.orde[i]) {
                        $scope.Sorted[_c] = $scope.weekDays[_i];
                        _c++;
                    }
                }
            }
        }; 

and printed $scope.Sorted. It prints what i said.

Is there any way to simplyfy this or any other mehtods in angularjs?

Upvotes: 1

Views: 2428

Answers (3)

runTarm
runTarm

Reputation: 11547

You could use a built-in orderBy filter like this:

app.controller('MainCtrl', function($scope, orderByFilter) {

  $scope.weekDays = [{
    "day": "TUESDAY",
    "count": 10
  }, {
    "day": "MONDAY",
    "count": 20
  }, {
    "day": "WEDNESDAY",
    "count": 30
  }, {
    "day": "SUNDAY",
    "count": 60
  }];

  var dateOrders = ["SUNDAY", "MONDAY", "FRIDAY", "TUESDAY", "WEDNESDAY"];

  $scope.Sorted = orderByFilter($scope.weekDays, function(item) {
    return dateOrders.indexOf(item.day);
  });
});

Example Plunker: http://plnkr.co/edit/IZfiavmZEpHf4hILdjQs?p=preview

Upvotes: 1

Manish Kr. Shukla
Manish Kr. Shukla

Reputation: 4477

Well, for all such ordering and filtering operations, i usually go for any available library..

I heavily recommend this one : http://jscriptlinq.codeplex.com/..

It's pretty simple to sort your collection :

var arr = [{id: 'a', pos: 5},
    {id: 'd', pos: 2},
    {id: 'b', pos: 4},
    {id: 'c', pos: 3},
    {id: 'e', pos: 1}];

// col1 = ['e', 'd', 'c', 'b', 'a']
var col1 = $linq(arr)
    .orderBy(function (x) { return x.pos; })
    .select(function (x) { return x.id; })
    .toArray();

// col2 = ['a', 'b', 'c', 'd', 'e']
var col2 = $linq(arr)
    .orderBy("x => x.id")
    .select("x => x.id")
    .toArray();

Upvotes: 0

maurycy
maurycy

Reputation: 8465

I've made you a working plunker with an

$scope.weekDays = [
        {
            "id" : 3,
            "day" : "TUESDAY",
            "count": 10
        },
        {
            "id" : 2,
            "day" : "MONDAY",
            "count": 20
        },
        {
            "id" : 4,
            "day" : "WEDNESDAY",
            "count": 30
        },
        {
            "id" : 1,
            "day" : "SUNDAY",
            "count": 60
        }];
$scope.orderedWeekDays = $filter('orderBy')($scope.weekDays, 'id')

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

Upvotes: 0

Related Questions