当某些项目具有未知值时,排序数组无法正常工作
Sorting array doesn't work properly when some items have unknown values
假设我有对象数组
[
{
"name": "John",
"height": "172",
"weight": "87"
},
{
"name": "Tom",
"height": "167",
"weight": "67"
},
{
"name": "James",
"height": "156",
"weight": "uknown"
},
{
"name": "Jeremy",
"height": "unknown",
"weight": "85"
},
]
现在,如果我想按高度对它们进行排序,我可以这样做 persons.sort((a, b) => b.height - a.height);
同样,如果我想按重量对它们进行排序,我也可以这样做,但重量 persons.sort((a, b) => b.weight - a.weight);
到目前为止,这一切正常,但有些值是未知的,如果我将此数组保持在某种状态并按未知值对它们进行排序,则会弄乱它并且无法正确排序。如果有比 4 更多的对象,它会排序到某个点,但一旦它碰到一个未知 属性 的对象,它就会把它弄乱,然后数组的其余部分就不会正确排序。我怎样才能克服这个?也许做一些事情将价值未知的项目推到底部?
您所要做的就是处理 height/weight 为 unknown
且 return 为适当值
的特定情况
例如:
const items = [
{
"name": "John",
"height": "172",
"weight": "87"
},
{
"name": "Tom",
"height": "167",
"weight": "67"
},
{
"name": "James",
"height": "156",
"weight": "uknown"
},
{
"name": "Jeremy",
"height": "unknown",
"weight": "85"
},
{
"name": "Jeremy2",
"height": "162",
"weight": "85"
},
{
"name": "Jeremy3",
"height": "unknown",
"weight": "85"
},
];
items.sort((a,b) => {
if(b.height === 'unknown' && a.height ==='unknown') return 0;
else if(b.height === 'unknown') return -1;
else if(a.height === 'unknown') return 1;
else return a.height - b.height;
});
console.log(items);
比较器函数 compare(a, b) 的工作原理如下:
if return value sort > 0, sort b before a.
else if return value sort < 0, sort a before b.
else if return value === 0 keep original order of a and b.
编辑:
如果处理逗号分隔的字符串,首先转换为数字:
items.sort((a,b) => {
if(b.height === 'unknown' && a.height ==='unknown') return 0;
else if(b.height === 'unknown') return -1;
else if(a.height === 'unknown') return 1;
else return parseFloat(a.height.replace(/,/g, '')) -
parseFloat(b.height.replace(/,/g, ''))
});
假设我有对象数组
[
{
"name": "John",
"height": "172",
"weight": "87"
},
{
"name": "Tom",
"height": "167",
"weight": "67"
},
{
"name": "James",
"height": "156",
"weight": "uknown"
},
{
"name": "Jeremy",
"height": "unknown",
"weight": "85"
},
]
现在,如果我想按高度对它们进行排序,我可以这样做 persons.sort((a, b) => b.height - a.height);
同样,如果我想按重量对它们进行排序,我也可以这样做,但重量 persons.sort((a, b) => b.weight - a.weight);
到目前为止,这一切正常,但有些值是未知的,如果我将此数组保持在某种状态并按未知值对它们进行排序,则会弄乱它并且无法正确排序。如果有比 4 更多的对象,它会排序到某个点,但一旦它碰到一个未知 属性 的对象,它就会把它弄乱,然后数组的其余部分就不会正确排序。我怎样才能克服这个?也许做一些事情将价值未知的项目推到底部?
您所要做的就是处理 height/weight 为 unknown
且 return 为适当值
例如:
const items = [
{
"name": "John",
"height": "172",
"weight": "87"
},
{
"name": "Tom",
"height": "167",
"weight": "67"
},
{
"name": "James",
"height": "156",
"weight": "uknown"
},
{
"name": "Jeremy",
"height": "unknown",
"weight": "85"
},
{
"name": "Jeremy2",
"height": "162",
"weight": "85"
},
{
"name": "Jeremy3",
"height": "unknown",
"weight": "85"
},
];
items.sort((a,b) => {
if(b.height === 'unknown' && a.height ==='unknown') return 0;
else if(b.height === 'unknown') return -1;
else if(a.height === 'unknown') return 1;
else return a.height - b.height;
});
console.log(items);
比较器函数 compare(a, b) 的工作原理如下:
if return value sort > 0, sort b before a.
else if return value sort < 0, sort a before b.
else if return value === 0 keep original order of a and b.
编辑:
如果处理逗号分隔的字符串,首先转换为数字:
items.sort((a,b) => {
if(b.height === 'unknown' && a.height ==='unknown') return 0;
else if(b.height === 'unknown') return -1;
else if(a.height === 'unknown') return 1;
else return parseFloat(a.height.replace(/,/g, '')) -
parseFloat(b.height.replace(/,/g, ''))
});