以下解决方案将在使用lambda函数时运行良好。对于某些业力测试用例失败,我必须避免在排序中使用lambda函数。我不知道如何克服这个问题。现在以下代码不是为我工作
var test=[{ID: "91",Name: "sgtue", standardID: "1"},
{ID: "41",Name: "asdfasdf", standardID: "2"},
{ID: "5", Name: "credd", standardID: "2"},
{ID: "2",Name: "dddawer", standardID: "2"},
{ID: "2",Name: "dsfadf", standardID: "3"},
{ID: "275", Name: "xcvcvc", standardID: "201"}
];
var groupOrder = [1,3,2,201];
var testSorted = test.sort(function (a, b) {groupOrder.indexOf(parseInt(a.standardID))-groupOrder.indexOf(parseInt(b.standardID))});
console.log(testSorted);
同样的概念将在以下链接上工作: fiddle working link
以下链接是我的实际问题: How do we customize the grouping from actual array of objects in angularjs
答案 0 :(得分:2)
您需要返回排序计算的结果。
var test = [{ ID: "91", Name: "sgtue", standardID: "1" }, { ID: "41", Name: "asdfasdf", standardID: "2" }, { ID: "5", Name: "credd", standardID: "2" }, { ID: "2", Name: "dddawer", standardID: "2" }, { ID: "2", Name: "dsfadf", standardID: "3" }, { ID: "275", Name: "xcvcvc", standardID: "201" }],
groupOrder = [1, 3, 2, 201];
test.sort(function(a, b) {
return groupOrder.indexOf(+a.standardID) - groupOrder.indexOf(+b.standardID);
// ^^^
});
console.log(test);

.as-console-wrapper { max-height: 100% !important; top: 0; }