Sorting parallel arrays in javascript - Stack Overflow

I have a couple of parallel arrays called names and sales.I have the user enter up to 100 salespeople

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745277467a4620121.html

相关推荐

  • Sorting parallel arrays in javascript - Stack Overflow

    I have a couple of parallel arrays called names and sales.I have the user enter up to 100 salespeople

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信