最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Sorting parallel arrays in javascript - Stack Overflow

programmeradmin0浏览0评论

I have a couple of parallel arrays called names and sales. I have the user enter up to 100 salespeople (names, obviously) and their sales. I have no problem printing these to a table. The catch (for me, anyway) is that they need to be sorted in descending order according to sales. I have made a function called sort which is coded (poorly - as I am just beginning to learn JavaScript) as:

function sort(names, sales) {
    var i = 0;
    var j = 0;
    var temp = 0;
    for (var i = 0; i < sales.length - 1; i++) {
        var min = i;
        for (var j = i + 1; j < array.length; j++)
        if (sales[j] < (sales[min])) min = j;
        temp = sales[i];
        sales[i] = sales[min];
        sales[min] = temp;
        temp = names[i];
        names[i] = names[min];
        names[min] = temp;
    }
}

I am in need of some help here, obviously. Can anyone lend a hand to point out the (no doubt numerous) errors?

We have been instructed to write our own sort. Sales and names are input through two different functions (getName() and getSales()) using prompts.

I have a couple of parallel arrays called names and sales. I have the user enter up to 100 salespeople (names, obviously) and their sales. I have no problem printing these to a table. The catch (for me, anyway) is that they need to be sorted in descending order according to sales. I have made a function called sort which is coded (poorly - as I am just beginning to learn JavaScript) as:

function sort(names, sales) {
    var i = 0;
    var j = 0;
    var temp = 0;
    for (var i = 0; i < sales.length - 1; i++) {
        var min = i;
        for (var j = i + 1; j < array.length; j++)
        if (sales[j] < (sales[min])) min = j;
        temp = sales[i];
        sales[i] = sales[min];
        sales[min] = temp;
        temp = names[i];
        names[i] = names[min];
        names[min] = temp;
    }
}

I am in need of some help here, obviously. Can anyone lend a hand to point out the (no doubt numerous) errors?

We have been instructed to write our own sort. Sales and names are input through two different functions (getName() and getSales()) using prompts.

Share Improve this question edited Mar 16, 2011 at 3:12 Yi Jiang 50.2k16 gold badges139 silver badges136 bronze badges asked Mar 16, 2011 at 2:53 unitunit 4151 gold badge8 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

First, why not use a single two-dimensional array, say, SalesArray, for example:

[ ['someName', 'someSale'], ['someName2', 'someSale2'], ]

Next, simply inspect SalesArray[i][1] while sorting.

As for sorting, try implementing bubblesort, especially if you're new to sorting algorithms.

Why not just store both the name and sales in a single object? Then everything is in one array.

// Declare array
var people = new Array();

// Somewhere in a loop to add people...
var person = {
    name: "jdmichal",
    sales: 1000
};
people.push(person);

// Now sort based on the sales property in each object.
发布评论

评论列表(0)

  1. 暂无评论