简体   繁体   English

如何在JavaScript中对串联数组进行排序?

[英]How do I sort a concatenated array in JavaScript?

My code: 我的代码:

var company=new Array("Kestrel Moon:","BB:");
var basicPri=new Array(1165,1231);

for(var i=0;i<15;i++){
var companyTotal=company[i].concat(basicPri[…

document.write(""+companyTotal+"")

It shows on the screen: 它显示在屏幕上:

Kestrel Moon: 1165

BB: 1231

I want to sort the array so that it goes ascending order of highest value of price so it should display it as: 我想对数组进行排序,以便它按价格的最高值升序排列,因此应将其显示为:

BB: 1231
Kestrel Moon: 1165

A normal sort would not do it as it would sort the prices but the company names stay where they are, how do I sort both arrays so it would display what I want to display? 普通排序不会这样做,因为它将对价格进行排序,但是公司名称保持不变,如何对两个数组进行排序,以便显示我想要显示的内容?

Thank You 谢谢

typically, you would group the name with number: 通常,您将名称与数字分组:

function sortNumber(a,b)
{
    return b[1] - a[1];  // comparing using 2nd element
}

var n = [["ccc", 10.23], ["www", 23.12], ["mmm", 0.56]];
document.write(n.sort(sortNumber));

output: 输出:

www,23.12,ccc,10.23,mmm,0.56

and if you use jQuery, you can create a table from the result: 如果使用jQuery,则可以根据结果创建一个表:

function sortNumber(a,b)
{
    return b[1] - a[1];  // comparing using 2nd element
}

var n = [["ccc", 10.23], ["www", 23.12], ["mmm", 0.56]];
sorted = n.sort(sortNumber);

$('body').append('<table id="thetable"></table>');
$('#thetable').css({borderCollapse: 'collapse'})
for (i = 0; i < sorted.length; i++) {
    $('#thetable').append('<tr><td>' + sorted[i][0] + '</td><td>' + sorted[i][1] + '</td></tr>')
}
$('td').css({ border: '1px solid #ccc', padding: '0.2em 1em' });

A normal sort would not do it as it would sort the prices but the company names stay where they are ... 普通排序不会这样做,因为它将对价格进行排序,但是公司名称保持不变。

In this case, an array of objects would probably be a better data structure for your data. 在这种情况下,对象数组可能是您的数据更好的数据结构。 Consider the example below: 考虑下面的示例:

var dict = [
  {company: 'Kestrel Moon:', basicPri: '1165'},
  {company: 'BB:', basicPri: '1231'}
];

var sorted = dict.sort(function(a, b) {
  return a.company.localeCompare(b.company);
});

console.log(sorted[0].company + ' ' + sorted[0].basicPri);
console.log(sorted[1].company + ' ' + sorted[1].basicPri);

// Prints:
// ------------------
// BB: 1231
// Kestrel Moon: 1165

Here's a possibility using your code as base (ie 2 arrays): 这有可能使用您的代码作为基础(即2个数组):

// comparaison functor
function sorter(a, b) {
    return a.name.localeCompare(b.name);
}

// build a sorted array of object like
// [ { "name": "foo", price: 123 }, ... ]
// sorted by company name (Locale aware order)
function mysort(comp, prices) {
    var res = [];

    for (var i = 0; i < comp.length; i++) {
        res.push({"name": comp[i], "price": prices[i]});
    }

    return res.sort(sorter);
}

var company = ["Kestrel Moon:", "BB:"];
var basicPri = [1165, 1231];
var companyTotal = "";
var arr = mysort(company, basicPri);

for (var i in arr) {
    companyTotal += arr[i].name + " " + arr[i].price + "<br/>";
}

document.write(companyTotal);

Tested on chrome 6.0.427.0 dev 在Chrome 6.0.427.0开发人员上测试

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

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