【发布时间】:2019-03-27 10:02:42
【问题描述】:
我正在从服务器获取数组,如下所示。
[
{
id: '508',
class: 'class1',
value: '6.0',
percentage: '8.90',
color: 'black'
},
{
id: '509',
class: 'class2',
value: '14,916',
percentage: '2.40',
color: 'black'
},
{
id: '510',
class: 'class3',
value: '14,916',
percentage: '56.40',
color: 'black'
},
{
id: '511',
class: 'class',
value: '4,916',
percentage: '2.40',
color: 'black'
}
]
从上面的列表中,我必须将最大百分比值显示为最低值。
所以,我尝试如下。
if (jsonData) {
const sortedArray = orderBy(
jsonData,
['percentage'],
['desc']
);
console.log('sortedArray is ', sortedArray);
}
它再次以相同的顺序出现,而不是从最大值到最小值。
有什么建议吗?
【问题讨论】:
-
什么是
orderBy -
你有奇怪的引用。这也不是有效的 JSON。此外,您正在比较 strings,因此您可能会得到这些字符串的顺序。
-
maximum percentage value to lowest?和descending order?我无法理解。请也发布预期的输出? -
从大到小排列的降序
标签: javascript sorting descendant