0

I know that it is possible to sort a table in angular with two column separately, i.e. first sort by name then sort by family.

https://plnkr.co/edit/bJIIlmGLyGWsxD5AQti0

var friends = [
  {name: 'a',   family: 'z',  age: 10},
  {name: 'z',   family: 'b',  age: 19},
  {name: 'c',   family: 'z',  age: 21},
  {name: 'z',   family: 'd',  age: 35},
  {name: 'z',  family: 'e',  age: 29}
];

$scope.propertyName = 'age';
$scope.reverse = true;
$scope.friends = orderBy(friends, $scope.propertyName, $scope.reverse);

$scope.sortBy = function(propertyName) {
  $scope.reverse = (propertyName !== null && $scope.propertyName === propertyName)
      ? !$scope.reverse : false;
  $scope.propertyName = propertyName;
  $scope.friends = orderBy(friends, ['name','family'], $scope.reverse);
};
}]);
})(window.angular);

I wonder if it is possible to sort them by combining the two fields, e.g order by "name" and "family" alphabetically, so it is appearing as "a,b,c,d,e"?

Claies
  • 22,124
  • 4
  • 53
  • 77
rawData
  • 729
  • 4
  • 10
  • 17

2 Answers2

1
<div ng-repeat="student in students | orderBy:['name','family']">
    {{student.name}}-{{student.family}}
</div>
Morteza QorbanAlizade
  • 1,520
  • 2
  • 19
  • 35
  • in this case, it is ordering by name and then family, it is not score them base on the combination of these two fields. so I like to achieve "a b c d e f " not "a c b d" – rawData Sep 19 '16 at 10:26
1

You can simply pass an Array to angular orderBy filter, this way:

ng-repeat="friend in friends | orderBy:['name', 'family']"

UPDATE: after question change by OP, I ask you to better state the requested ordering function, since it is not very clear (to me) from the question...

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • i am looking to score the order by combining two fields, so it will appear as below {name: 'a', family: 'z', age: 10}, {name: 'z', family: 'b', age: 19}, {name: 'c', family: 'z', age: 21}, {name: 'z', family: 'd', age: 35}, {name: 'z', family: 'e', age: 29} – rawData Sep 19 '16 at 10:30
  • I was not asking for an example, but for a definition... :-) – MarcoS Sep 19 '16 at 10:31