【问题标题】:Sorting array by float value按浮点值排序数组
【发布时间】:2017-12-19 09:23:58
【问题描述】:

我在按浮点值对数组进行排序时遇到了一些问题。我在网上搜索过,我知道我必须使用比较功能,但我在理解这个概念时遇到了问题。

我正在使用此代码读取 xlxs 文件并将所需的值推送到更简单的数组。我需要按 top2box 键的值对这个数组进行排序,所以最大值是键 0。

这是我当前的代码

// data is an array of arrays, lets loop through it to sort. The array contains each row of my xlxs file.

    var hon = [] //array that will hold the sorted entries from the excel file        

    for(var i = 0; i < data.length; i++) {

        // we dont want empty data. each row has a key named Business Unit. In empty rows the value of the key is the name of the key. We dont need that data.
        if (data[i][3] != '' && data[i][0] != 'Business Unit') {

            // data belongs to an actual person
            // round top2box to display the percentage.

            // push the values we need to a simpler array
            hon.push({
                country: data[i][0],
                team: data[i][2],
                name: data[i][3],
                top2box: data[i][4],
                sumofvotes: data[i][5]
            })
        }
    }

    // loop done lets sort each array entry by top2box value. So highest top2box is the first entry of the array
    hon.sort(function(a,b) { return a.top2box - b.top2box;});

    // show the result.
    console.log(JSON.stringify(hon, null, 4));

但是,在显示结果时,所有 top2box 条目都已更改为“1”并且未排序(可能也是由于这个原因)

hon 的值是一个浮点数,稍后需要显示为百分比。以下是一些示例值。我需要保持这些准确的值,但将它们从高到低排序,这样我就可以循环遍历数组并稍后将它们显示为 html。

"country": "Norway",
"team": "NO-Aftersales",
"name": "Andersen, Dennis",
"top2box": "0.47368421052631599",
"sumofvotes": "19"

还有一个

"country": "Sweden",
"team": "SE-AS",
"name": "Vuong, Adele",
"top2box": "0.51515151515151503",
"sumofvotes": "33"

解决方案

原来是 JSON.stringify();是问题的根源。从 console.log 中删除它。因此,它改为 console.log(hon) 显示正确的数据并正确排序。 Json stringify 对浮点数的处理不是很好。

【问题讨论】:

  • 所以最高的 top2box 是第一个条目那么它应该是b - a。另请说明top2box 条目已更改为“1”
  • 你初始化了吗?
  • 对不起。 Hon 已初始化并正确显示 yes。 @Rajesh 看看这个输出的屏幕截图imgur.com/a/s3Qvy Top2Box 我应该显示百分比值。第二个屏幕截图显示了我想要维护但已排序的正确值。
  • 从您的屏幕截图看来,top2box 是一个字符串,您必须先解析它,然后再在 sort 中进行比较。尝试parseFloat(a.top2box) - parseFloat(b.top2box),您还可以使用排序值更新honhon = hon.sort()
  • @n0rd 问题不是由您共享的任何代码引起的。请查看您的完整代码,了解值的更新位置

标签: javascript arrays node.js sorting


【解决方案1】:

您需要像这样保存“排序”的结果:

var hon = [
{
    country: 'some country',
    team: 'some team',
    name: 'some name',
    top2box: 123123,
    sumofvotes: 123123
},
{
    country: 'some country2',
    team: 'some team2',
    name: 'some name2',
    top2box: 123,
    sumofvotes: 123
}
];

hon = hon.sort((a, b) => {return a.top2box - b.top2box}); // save the sort result
// (a - b) = Ascending sort, (b - a) = Descending sort

console.log(hon);

您可以在此处阅读更多关于排序的信息 - Array#Sort 和有关箭头函数的信息 - Functions#ArrowFunctions

【讨论】:

  • 感谢您的回复!我试过这个,但它会将 top2box 的所有值更改为 1。{ "country": "Denmark", "team": "DK-Aftersales", "name": "Skaaning, Rasmus", "top2box": "1", "sumofvotes": "4" }, { "country": "Sweden", "team": "", "name": "Rosenius, Niklas", "top2box": "1", "sumofvotes": "1" } 最初它们是浮点值。示例:0.241232 我需要维护它,但按从高到低排序。
  • 'Sort' 不能改变 prop 的类型或值。要按从高到低对它们进行排序,您需要使用“b - a”。
  • (a, b) =&gt; {return a.top2box - b.top2box} 太过分了(至少对我而言)。可以直接(a, b) =&gt; a.top2box - b.top2box
  • @Rajesh 我现在排序如下:hon = hon.sort((a, b) =&gt; a.top2box - b.top2box); 输出中的所有 top2box 值现在都是“1”,而不是原来的浮点值。
  • @n0rd,我认为您在模块中某处进行了 hon 或 top2box 的转换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 2013-12-03
  • 2018-09-22
  • 2021-04-26
  • 2012-12-24
相关资源
最近更新 更多