Javascript 2D数组排序-按数值 [英] Javascript 2D array sorting - by numerical value

查看:62
本文介绍了Javascript 2D数组排序-按数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过另外几篇与这个问题类似的帖子,但是都没有适合我的情况,并且迷失了解决方法(例如

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)

我将解释问题-我在JavaScript中面临的问题;

I will explain the problem - which I am facing within JavaScript;

我有两个数组,用于在第三方产品中创建图表.我有以下变量;

I have two arrays, which I am using to create a chart within a 3rd party product. I have the following variables;

label [股票A",股票B",股票C",股票D"]

label["Stock A", "Stock B", "Stock C", "Stock D"]

value ["200","1000","900","800"]

value["200", "1000", "900", "800"]

我想要2D阵列中的

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

sorted["Stock B, 1000","Stock C, 900", "Stock D, 800", "Stock 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.

我上周在某处发现了另一条漫长的路,但是它正在将诸如100、1000之类的值彼此相邻排序-我认为这不是整数,而是这种运气.我在周五大肆删除了该代码.

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. I deleted the code in a rage on Friday.

我希望那里的某种灵魂可以帮助新的编码员. O值以字符串形式出现,因此需要将其转换为int.

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.

数组中的条目数量是无限的,一旦我正确地对它们进行了排序,我需要保持前10名,然后将其他条目合计为一组-这样我将有11条条目.但是,在解决该问题之前,我需要先进行排序.

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. However I need to sort before I can tackle that.

谢谢.

推荐答案

我想要2D阵列中的

I would like this in a 2D array

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

这不是2D数组,而是一维字符串数组.这将是一个2D数组:

That's not a 2D array, that's a single dimensional array of strings. This would be a 2D array:

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;
});

这篇关于Javascript 2D数组排序-按数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆