按属性对对象数组进行排序(使用自定义顺序,而不是按字母顺序) [英] Sort an array of object by a property (with custom order, not alphabetically)

查看:28
本文介绍了按属性对对象数组进行排序(使用自定义顺序,而不是按字母顺序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想就这个小问题寻求您的帮助.

我想根据 code而不是按字母顺序对这个数组进行排序.(我用粗体指定了这个,但最终还是被标记了,人们甚至不关心阅读问题)

例如,我想要所有的 green 对象,然后是所有的 blue 对象,然后是所有的 red 对象.最好的方法是什么?

<预><代码>[{代码:红色",值:0},{代码:蓝色",值:0},{代码:红色",值:0},{ 代码:绿色",值:0},{代码:蓝色",值:0},{代码:红色",值:0},{ 代码:绿色",值:0},{ 代码:蓝色",值:0}]

是否可以使用 sort 函数来做到这一点?在那种情况下,条件是什么?

解决方案

您可以为想要的订单选择一个对象.

var array = [{ code: "RED", value: 0 }, { code: "BLUE", value: 0 }, {代码:红色",值:0 },{ 代码:绿色",值:0 },{ 代码:蓝色",值:0 },{ 代码:红色",值:0 },{ 代码:"GREEN", value: 0 }, { code: "BLUE", value: 0 }],订单 = { 绿色:1,蓝色:2,红色:3};数组排序(函数(a,b){退货订单[a.code] - 订单[b.code];});console.log(array);

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

对于未知的颜色/值,您可以使用默认值

  • 0,用于排序到顶部或
  • Infinity 用于排序到最后,
  • 或用于在其他组之间进行排序的任何其他值.

最后,您可以使用其他排序部分对特殊处理的项目进行排序,链接为 逻辑 OR ||.

var array = [{ code: "YELLOW", value: 0 }, { code: "BLACK", value: 0 }, {代码:红色",值:0 },{ 代码:蓝色",值:0 },{ 代码:红色",值:0 },{ 代码:绿色",值:0 },{ 代码:"BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }],order = { 绿色:1,蓝色:2,红色:3,默认值:无限 };数组排序(函数(a,b){return (order[a.code] || order.default) - (order[b.code] || order.default) ||a.code.localeCompare(b.code);});console.log(array);

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

I would like to get your help about this little problem.

I have like to order this array depending on the code value but not in alphabetical order. (I specified this in bold but got eventually flagged anyway, people don't even care about reading the question)

For example I would like to have all the green objects, then all the blue ones and then all the red ones. What is the best way to do that?

[
    { code: "RED", value: 0},
    { code: "BLUE", value: 0},
    { code: "RED", value: 0},
    { code: "GREEN", value: 0},
    { code: "BLUE", value: 0},
    { code: "RED", value: 0},
    { code: "GREEN", value: 0},
    { code: "BLUE", value: 0}
]

Is it possible to do that with the sort function? What would the condition be in that case?

解决方案

You could take an object for the wanted order.

var array = [{ code: "RED", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }],
    order = { GREEN: 1, BLUE: 2, RED: 3 };
    
array.sort(function (a, b) {
    return order[a.code] - order[b.code];
});

console.log(array);

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

For unknow colors/values, you could use a default value with

  • 0, for sorting to top or
  • Infinity for sorting to the end,
  • or any other value for sorting inbetween the other groups.

At last you could sort the special treated items with an other sorting part, chained with logical OR ||.

var array = [{ code: "YELLOW", value: 0 }, { code: "BLACK", value: 0 }, { code: "RED", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }],
    order = { GREEN: 1, BLUE: 2, RED: 3, default: Infinity };
    
array.sort(function (a, b) {
    return (order[a.code] || order.default) - (order[b.code] || order.default) || a.code.localeCompare(b.code);
});

console.log(array);

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

这篇关于按属性对对象数组进行排序(使用自定义顺序,而不是按字母顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆