1

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?

1
  • you could add unique, incrementing key and then oder by it Commented Aug 6, 2014 at 10:07

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's a clever way, didn't know you cna inject and use single filter like that
0

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

3 Comments

expecting sorting order is in another property. If it is like the above { "id" : 3, "day" : "TUESDAY", "count": 10 },.. we can print by | orderBy: weekDays.id.
that's what I'm doing in my plunker, i provided two versions
i am thinking about how to do without id in weekDays object. the order object is a separate one which says the days. ie.. order by the given object. like runTarm suggested
0

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();

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.