如何使用 angularjs 中的 sortBy 过滤器对波兰语字符进行排序?

How to sort Polish characters with the sortBy filter in angularjs?

我有问题。 Angularjs 1 方法中的 'sortBy' 不会按字母顺序对波兰语字符进行排序。应该排序为:Alfred、Ahris、Bert、Chora、Dora。其实还有:Alfred, Berta, Dora, Ąhris, Ćora.

index.html

<!DOCTYPE html>
<html ng-app="app">

  <head>
    <link data-require="bootstrap-css@*" data-semver="3.1.1" rel="stylesheet"
          href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
    <script data-require="angular.js@1.2.10" data-semver="1.2.14"
            src="http://code.angularjs.org/1.2.14/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script type="text/javascript" src="script.js"></script>
  </head>

  <body ng-controller="MainCtrl">

    <div>
      Order By:
      <select ng-model="sortorder">
          <option selected>Name</option>
          <option value="Age">Age</option>
      </select>
      <br>
      <table style="width:300px">
          <tr>
            <td>Name</td>
            <td>Age</td>      
          </tr>
          <tr ng-repeat="contact in contacts | orderBy:sortorder">
            <td>{{contact.Name}}</td>     
            <td>{{contact.Age}}</td>
          </tr>
      </table>              
      </div>  

  </body>

</html>

script.js

var app = angular.module('app', []);

app.controller('MainCtrl', ['$scope', function ($scope) {
  $scope.sortorder = 'Name';
  $scope.contacts = [
    {Name: 'Alfred', Age: 37},
    {Name: 'Berta', Age: 65},
    {Name: 'Ąhris', Age: 25},
    {Name: 'Dora', Age: 12},
    {Name: 'Ćora', Age: 12}
    ]
}]);

示例:http://plnkr.co/edit/ZwtGPSEvlFul6cNtn7hd?p=preview

orderBy 的默认比较器并不像您发现的那样区分语言环境。您可以通过传入自定义比较器来获得所需的订单。 The latter has been available since 1.5.7。我注意到您已将版本标记为 1.6 版,但在您的 Plunker 中您引用了 1.2.

比较器占用orderBy的第三个参数:

orderBy:<expression>:<reverse>:<comparator>

例如:

$scope.comparator = function (a, b) {
    if (a.type === 'string' && b.type === 'string') {
        return a.value.localeCompare(b.value);
    }
    return a.value - b.value;
};

如果您向 $scope.contacts 添加任何需要作为排序依据的额外属性,请确保扩展比较器以处理数据类型。

Here's a forked version of your Plunker demonstrating this.