英文:
How can we sort array object in JavaScript?
问题
我试图按颜色对对象数组进行排序。
我的代码:
const colors = [
{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}
]
const list = colors.sort((a) => a.color);
console.log(list);
如何实现这个目标?
英文:
I'm trying to sort a object array as per colors.
My code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const colors = [
{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}]
const list = colors.sort((a) => a.color);
console.log(list);
<!-- end snippet -->
How can I achieve this?
答案1
得分: 3
此代码将帮助您根据颜色值按升序排序。
const colors = [
{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}
]
const sortedColors = colors.sort((a, b) => {
if (a.color < b.color) {
return -1;
}
if (a.color > b.color) {
return 1;
}
return 0;
});
console.log(sortedColors);
如果您需要进一步的帮助,请随时告诉我。
英文:
This code will help you to sort in asc order based on color value.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const colors = [
{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}
]
const sortedColors = colors.sort((a, b) => {
if (a.color < b.color) {
return -1;
}
if (a.color > b.color) {
return 1;
}
return 0;
});
console.log(sortedColors);
<!-- end snippet -->
答案2
得分: 2
尝试这个,它会起作用。
您可以根据需求更改条件,谢谢。
const colors = [{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}
]
const result = colors.sort((a, b) => (a.color > b.color) ? 1 : ((b.color > a.color) ? -1 : 0))
console.log(result);
英文:
Try this, it will work.
You can change condition as per requirement, Thanks.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const colors = [{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}
]
const result = colors.sort((a, b) => (a.color > b.color) ? 1 : ((b.color > a.color) ? -1 : 0))
console.log(result);
<!-- end snippet -->
答案3
得分: 2
JavaScript中的Array.prototype.sort
接受一个带有两个参数的回调函数。
根据MDN,
compareFn(a, b) 返回值 | 排序顺序 |
---|---|
> 0 | 在b之后排序a |
< 0 | 在a之前排序b |
=== 0 | 保持a和b的原始顺序 |
然而,您可以使用内置的String.prototype.localeCompare
来比较两个字符串,而不是编写自己的比较函数。
const colors = [
{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}
]
const list = colors.sort((a, b) => a.color.localeCompare(b.color));
console.log(list);
英文:
Array.prototype.sort
in JavaScript takes in a callback with two parameters.
From MDN,
compareFn(a, b) return value | sort order |
---|---|
> 0 | sort a after b |
< 0 | sort a before b |
=== 0 | keep original order of a and b |
However, instead of writing your own comparison function, you can use the builtin String.prototype.localeCompare
to compare two strings.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const colors = [
{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}]
const list = colors.sort((a, b) => a.color.localeCompare(b.color));
console.log(list);
<!-- end snippet -->
答案4
得分: -1
Using sort()
, you want to compare two elements in the array, so you need to put two parameters in there.
const colors = [
{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}
]
const list = colors.sort((a, b) => a.color > b.color);
console.log(list);
英文:
Using sort()
, you want to compare two elements in the array, so you need to put two parameters in there.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const colors = [
{
"color": "purple",
"type": true
},
{
"color": "red",
"type": false
},
{
"color": "green",
"type": true
},
{
"color": "black",
"type": false
},
{
"color": "pink",
"type": true
}]
const list = colors.sort((a, b) => a.color > b.color);
console.log(list);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论