2011-02-17 140 views
0

在JavaScript中,我有2个数组。如何对基于二维数组的一维数组进行排序?

一个是一维数组,另一种是二维数组

一维数组的内容是:

a[0] = "Germany"; 
a[1] = "England"; 
a[2] = "America"; 
a[3] = "France"; 

二维数组内容是:

a[0][0] = "America"; 
a[1][0] = "England"; 
a[2][0] = "France"; 
a[3][0] = "Germany"; 

我怎样才能让一维数组的排列顺序与二维数组相同?

也就是说,我想一维数组的最终结果是:

a[0] = "America"; 
a[1] = "England"; 
a[2] = "France"; 
a[3] = "Germany"; 

是否有可能为我做这样的动作?

+1

这是否需要*排序*,因为这两个数组是不一样的,或者可能你只是* *扁平化的二维数组来获得相同的结果呢? – deceze 2011-02-17 03:09:18

回答

0

这两个数组是如何创建的?你可能会与地图更好......是这样的:

var a = {'America':'0', 'England':'1', 'France':'2', 'Germany':'3'}; 

然后你就可以访问它们,如:

a['America']得到0,或a['England']=4;

0

如果您已经为它们分配在另一个数组中有已排序的结构,为什么不直接将二维数组中的每个元素复制到一维数组中。

a[i] = a[i][0] 
//Where i goes from 0 to 3 in this case 

这将节省时间&计算资源

相关问题