Angular:按值列表排序集合

时间:2015-07-29 09:31:53

标签: javascript angularjs frontend lodash

如何根据来自其他数组的特定顺序对(Angular,Lodash)对象集合进行排序:

var list = [{name: "Name 10"}, 
            {name: "Name 20"},
            {name: "Name 321"},
            {name: "Name 41"}];

var orderList = ["Name 20", "Name 41", "Name 321", "Name 10"];

//result: {name: "Name 20"}, {name: "Name 41"},{name: "Name 321"},{name: "Name 10"}]

我需要将订单从orderList应用到list

c#Sort collection by another collection values

中的模拟

2 个答案:

答案 0 :(得分:1)

对于此示例,我们将使用filter指令呈现朋友列表。使用内置的orderBy <input ng-model="query" type="text" placeholder="Filter"> <ul ng-repeat="obj in list | filter:query | orderBy: 'name' "> <li>{{friend.name}}</li> </ul> 过滤器,我们将对客户端的好友列表进行过滤和排序。

import numpy as np
import matplotlib.pyplot as plt

coords = np.random.normal(0, 1, (2, 1000))

H, xedges, yedges = np.histogram2d(coords[0], coords[1], bins=50)

im = plt.imshow(H, interpolation='nearest', origin='low',
                extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])

答案 1 :(得分:1)

AngularJS支持按orderBy过滤器排序。我们需要传递表达式函数来过滤其他数组。请查看以下代码:

HTML:

{{list|orderBy:order}}

使用Javascript:

$scope.list = [{name: "Name 1"}, 
        {name: "Name 2"},
        {name: "Name 3"},
        {name: "Name 4"}];
$scope.orderList = ["Name 2", "Name 1", "Name 3", "Name 4"];
$scope.order = function(predicate) {
    return $scope.orderList.indexOf(predicate.name);
  };  

为了更好地理解我已经JSFiddle