排序在JavaScript并行阵列 [英] Sorting parallel arrays in javascript
问题描述
我有一对夫妇叫名字和销售并行阵列。我有用户输入最多100个销售人员(名称,很明显)和他们的销售。我没有问题,这些印刷到表。美中不足的(对我来说,无论如何)是他们需要根据销售递减顺序进行排序。我做了一个调用的函数排序
这是codeD(很差 - 因为我刚开始学习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 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?
我们已指示写我们自己的排序。销售和名称是通过两个不同的功能输入(的getName()
和 getSales()
)使用提示。
We have been instructed to write our own sort. Sales and names are input through two different functions (getName()
and getSales()
) using prompts.
推荐答案
首先,为什么不使用一个二维数组,也就是说,SalesArray,例如:
First, why not use a single two-dimensional array, say, SalesArray, for example:
[['someName','someSale'],
['someName2','someSale2'],
]
[ ['someName', 'someSale'], ['someName2', 'someSale2'], ]
接下来,只要检查SalesArray [I] [1],而排序。
Next, simply inspect SalesArray[i][1] while sorting.
至于排序,尝试实施冒泡的,特别是如果你是新来排序算法。
As for sorting, try implementing bubblesort, especially if you're new to sorting algorithms.
这篇关于排序在JavaScript并行阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!