AngularJS和orderby在ng-repeat中有特殊字符

时间:2022-12-02 17:09:45

I am having trouble ordering strings containing characters that are not in the English alphabet ( š,č,ž,..)

我有困难排序字符串不包含字符的英语字母(š、čž,. .)

Here is the fiddle: http://fiddle.jshell.net/vhhgh/

这是小提琴:http://fiddle.jshell.net/vhhgh/

The letters are from the Slovenian alphabet.

这些字母来自斯洛文尼亚字母。

4 个解决方案

#1


8  

It's been a while, but I found other solution: fiddle

已经有一段时间了,但我找到了其他解决办法:小提琴。

HTML:

HTML:

<div ng-app='test'>
  <h2>Users</h2>
  <div ng-controller="UsersCtrl">
    <ul>
      <li ng-repeat="user in users | localeCompareString">
        {{user.surname}} {{user.name}}
      </li>
    </ul>
  </div>
</div>

JS:

JS:

(function(angular) {
  'use strict';
  var test=angular.module('test',[])
.controller('UsersCtrl', ['$scope',function($scope) {
  $scope.users = [
    {name:'Ben', surname:'Živkovič'},
    {name:'Ken', surname:'AlGore'},
    {name:'Erica', surname:'Červ'},
    {name:'Jane', surname:'Šinigoj'},
    {name:'Kevin', surname:'Sort'},
    {name:'Roger', surname:'Willson'},
    {name:'Kim', surname:'Zorro'}
];
}]).filter('localeCompareString',function(){
    return function (items) {
         //window.console.log(items);
        items.sort(function (a, b) {
            return a.surname.localeCompare(b.surname);
        });
        return items;
      }; 
});

})(window.angular);        

#2


1  

Ordering arrays of strings with "foreign" letters isn't as easy to do as you might think. Actally, it can be a right pain in the ... to get right. The problem boils down to the fact that the Unicode charset contains (pretty much) all charactrers in existance, so a universal lexicographical sorting isn't possible since different countries all have different ways they expect the sorting to be handled.

用“外国”字母排序字符串数组并不像你想象的那么容易。事实上,在……得到正确的。问题的关键在于,Unicode字符集包含(相当多)所有字符的存在,因此,由于不同的国家都有不同的方法来处理排序,所以通用的词典排序是不可能的。

To get around this, I've found TCollator, a small library aiming at fixing that issue, very useful.

为了解决这个问题,我找到了TCollator,一个旨在解决这个问题的小型图书馆,非常有用。

#3


1  

You can compare two strings with the String.localeCompare() method. It's then easy to create your own filter to sort your array:

您可以将两个字符串与String.localeCompare()方法进行比较。然后很容易创建自己的过滤器来对数组进行排序:

MyApp.filter('myOrderBy', function () {
return function (array, property, reverse) {
    var result = array.sort(function (object1, object2) {
        if (angular.isUndefined(property)) {
            return object1.localeCompare(object2);
        }

        return object1[property].localeCompare(object2[property]);
    });

    return reverse ? result.reverse() : result;
};

});

});

#4


0  

Starting from AngularJS 1.5.7, orderBy takes an optional comparator function. The docs contain an example involving a locale-sensitive comparator.

orderBy从AngularJS 1.5.7开始采用可选的比较器函数。文档包含一个涉及位置敏感比较器的示例。

#1


8  

It's been a while, but I found other solution: fiddle

已经有一段时间了,但我找到了其他解决办法:小提琴。

HTML:

HTML:

<div ng-app='test'>
  <h2>Users</h2>
  <div ng-controller="UsersCtrl">
    <ul>
      <li ng-repeat="user in users | localeCompareString">
        {{user.surname}} {{user.name}}
      </li>
    </ul>
  </div>
</div>

JS:

JS:

(function(angular) {
  'use strict';
  var test=angular.module('test',[])
.controller('UsersCtrl', ['$scope',function($scope) {
  $scope.users = [
    {name:'Ben', surname:'Živkovič'},
    {name:'Ken', surname:'AlGore'},
    {name:'Erica', surname:'Červ'},
    {name:'Jane', surname:'Šinigoj'},
    {name:'Kevin', surname:'Sort'},
    {name:'Roger', surname:'Willson'},
    {name:'Kim', surname:'Zorro'}
];
}]).filter('localeCompareString',function(){
    return function (items) {
         //window.console.log(items);
        items.sort(function (a, b) {
            return a.surname.localeCompare(b.surname);
        });
        return items;
      }; 
});

})(window.angular);        

#2


1  

Ordering arrays of strings with "foreign" letters isn't as easy to do as you might think. Actally, it can be a right pain in the ... to get right. The problem boils down to the fact that the Unicode charset contains (pretty much) all charactrers in existance, so a universal lexicographical sorting isn't possible since different countries all have different ways they expect the sorting to be handled.

用“外国”字母排序字符串数组并不像你想象的那么容易。事实上,在……得到正确的。问题的关键在于,Unicode字符集包含(相当多)所有字符的存在,因此,由于不同的国家都有不同的方法来处理排序,所以通用的词典排序是不可能的。

To get around this, I've found TCollator, a small library aiming at fixing that issue, very useful.

为了解决这个问题,我找到了TCollator,一个旨在解决这个问题的小型图书馆,非常有用。

#3


1  

You can compare two strings with the String.localeCompare() method. It's then easy to create your own filter to sort your array:

您可以将两个字符串与String.localeCompare()方法进行比较。然后很容易创建自己的过滤器来对数组进行排序:

MyApp.filter('myOrderBy', function () {
return function (array, property, reverse) {
    var result = array.sort(function (object1, object2) {
        if (angular.isUndefined(property)) {
            return object1.localeCompare(object2);
        }

        return object1[property].localeCompare(object2[property]);
    });

    return reverse ? result.reverse() : result;
};

});

});

#4


0  

Starting from AngularJS 1.5.7, orderBy takes an optional comparator function. The docs contain an example involving a locale-sensitive comparator.

orderBy从AngularJS 1.5.7开始采用可选的比较器函数。文档包含一个涉及位置敏感比较器的示例。