简体   繁体   English

Javascript 2D数组排序-按数值

[英]Javascript 2D array sorting - by numerical value

I've seen another couple of posts similar to this question, but none work for my scenario and at a lost how to solve this (such as Sort a 2D array by the second value ) 我看过另外几篇与这个问题类似的文章,但都不适用于我的情况,并且不知道如何解决此问题(例如, 按第二个值对2D数组进行排序

I will explain the problem - which I am facing within JavaScript; 我将解释问题-我在JavaScript中面临的问题;

I have two arrays, which I am using to create a chart within a 3rd party product. 我有两个数组,用于在第三方产品中创建图表。 I have the following variables; 我有以下变量;

label["Stock A", "Stock B", "Stock C", "Stock D"] 标签[“股票A”,“股票B”,“股票C”,“股票D”]

value["200", "1000", "900", "800"] 值[“ 200”,“ 1000”,“ 900”,“ 800”]

I would like this in a 2D array 我想要二维数组

sorted["Stock B, 1000","Stock C, 900", "Stock D, 800", "Stock A, 200"] 已排序[“股票B,1000”,“股票C,900”,“股票D,800”,“股票A,200”]

I want to sort by the numerical value, if possible. 如果可能的话,我想按数值排序。 The problem is that when I use the solution within the question posted above, it only compares the value with one other value. 问题是,当我在上面发布的问题中使用解决方案时,它仅将值与另一个值进行比较。 I need this to go through each value already stored within the array. 我需要它来检查数组中已经存储的每个值。

I found another really long winded way somewhere last week, but it was sorting values such as 100, 1000 next to each other - which I thought was down to not being integers etc but no such luck. 上周我在某处发现了另一条漫长的路,但它是将诸如100、1000之类的值彼此相邻排序-我认为这不是整数,而是这种运气。 I deleted the code in a rage on Friday. 我星期五大肆删除代码。

I was hoping that some kind soul at there could help a new coder. 我希望那里的某种灵魂可以帮助新的编码器。 O the values come in as strings so they will need to be converted to ints. O值以字符串形式出现,因此需要将其转换为int。

There is an unlimited amount of entries in the array, and once I've sorted these correctly I need to keep the top 10 and then group the others into a group with a total - so I would have 11 entries. 数组中的条目数量是无限的,一旦我对这些条目进行了正确的排序,我就需要保留前10名,然后将其他条目合计为一组-这样我就有11条条目。 However I need to sort before I can tackle that. 但是,在解决该问题之前,我需要进行排序。

Thanks in advance. 提前致谢。

I would like this in a 2D array 我想要二维数组

sorted["Stock B, 1000","Stock C, 900", "Stock D, 800", "Stock A, 200"]

That's not a 2D array, that's a single dimensional array of strings. 那不是二维数组,而是一维字符串数组。 This would be a 2D array: 这将是一个2D数组:

sorted = [["Stock B", 1000], ["Stock C", 900], ["Stock D", 800], ["Stock A", 200]];

...which you can sort by the second value like this: ...您可以按第二个值进行排序,如下所示:

sorted.sort(function(a, b) {
    var avalue = a[1],
        bvalue = b[1];
    if (avalue < bvalue) {
        return -1;
    }
    if (avalue > bvalue) {
        return 1;
    }
    return 0;
});

But you may be better off with a single-dimensional array of objects: 但是使用一维对象数组可能会更好:

sorted = [
    {name: "Stock B", quantity: 1000},
    {name: "Stock C", quantity: 900},
    {name: "Stock D", quantity: 800},
    {name: "Stock A", quantity: 200}
];

...which you can sort like this: ...您可以像这样排序:

sorted.sort(function(a, b) {
    var avalue = a.quantity,
        bvalue = b.quantity;
    if (avalue < bvalue) {
        return -1;
    }
    if (avalue > bvalue) {
        return 1;
    }
    return 0;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM