Sorting Parallel Arrays In Javascript
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 t
Solution 1:
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.
Solution 2:
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.
Post a Comment for "Sorting Parallel Arrays In Javascript"