Suppose I have two tables A and B and each one has only 1 column.
假设我有两个表A和B,每个表只有一列。
Each row in table A corresponds to each row in B, but I don't want them to be in one table.
表A中的每一行对应于B中的每一行,但我不希望它们在一个表中。
A B
------ ----------
car automobile
bike train
When I sort A alphabetically I should get
当我按字母顺序排序时,我应该得到
A B
------ ----------
bike train
car automobile
2 个解决方案
#1
1
Since the items in table A have a one-to-one correspondence to the items in table B, it's better to use an object to represent this data. An array of these objects represents the entire dataset. Whenever a sort is needed, just sort this array, and repopulate the tables.
由于表A中的项与表B中的项具有一对一的对应关系,因此最好使用对象来表示此数据。这些对象的数组表示整个数据集。每当需要排序时,只需对此数组进行排序,然后重新填充表。
// first represents the item in table A
// second represents the item in table B
function Transporter(first, second) {
this.first = first;
this.second = second;
}
Then create a custom sort function that only compares the property first
for sorting an array of Transporter
objects.
然后创建一个自定义排序函数,该函数仅首先比较属性以排序Transporter对象数组。
function compare(a, b) {
if(a.first < b.first) {
return -1;
}
else if(a.first > b.first) {
return 1;
}
return 0;
}
And a test run:
并且测试运行:
var transporters = [];
transporters.push(new Transporter("car", "automobile"));
transporters.push(new Transporter("bike", "train"));
console.log(transporters); // [0] => (car:automobile), [1] => (bike:train)
transporters.sort(compare);
console.log(transporters); // [0] => (bike:train), [1] => (car:automobile)
When the sort is done, update both tables.
排序完成后,更新两个表。
Or, alternatively use any existing script or plugin. Here's one for jQuery: http://tablesorter.com/docs/
或者,或者使用任何现有的脚本或插件。这是jQuery的一个:http://tablesorter.com/docs/
#2
1
Surely, this is as simple as two calls, assuming a sufficient abstraction.
当然,假设有足够的抽象,这就像两个调用一样简单。
sortingModule.sort( document.getElementById('table-a') );
sortingModule.sort( document.getElementById('table-b') );
I think you need to give us more details. Have you written the sorting mechanism?
我想你需要给我们更多细节。你有没有写过排序机制?
#1
1
Since the items in table A have a one-to-one correspondence to the items in table B, it's better to use an object to represent this data. An array of these objects represents the entire dataset. Whenever a sort is needed, just sort this array, and repopulate the tables.
由于表A中的项与表B中的项具有一对一的对应关系,因此最好使用对象来表示此数据。这些对象的数组表示整个数据集。每当需要排序时,只需对此数组进行排序,然后重新填充表。
// first represents the item in table A
// second represents the item in table B
function Transporter(first, second) {
this.first = first;
this.second = second;
}
Then create a custom sort function that only compares the property first
for sorting an array of Transporter
objects.
然后创建一个自定义排序函数,该函数仅首先比较属性以排序Transporter对象数组。
function compare(a, b) {
if(a.first < b.first) {
return -1;
}
else if(a.first > b.first) {
return 1;
}
return 0;
}
And a test run:
并且测试运行:
var transporters = [];
transporters.push(new Transporter("car", "automobile"));
transporters.push(new Transporter("bike", "train"));
console.log(transporters); // [0] => (car:automobile), [1] => (bike:train)
transporters.sort(compare);
console.log(transporters); // [0] => (bike:train), [1] => (car:automobile)
When the sort is done, update both tables.
排序完成后,更新两个表。
Or, alternatively use any existing script or plugin. Here's one for jQuery: http://tablesorter.com/docs/
或者,或者使用任何现有的脚本或插件。这是jQuery的一个:http://tablesorter.com/docs/
#2
1
Surely, this is as simple as two calls, assuming a sufficient abstraction.
当然,假设有足够的抽象,这就像两个调用一样简单。
sortingModule.sort( document.getElementById('table-a') );
sortingModule.sort( document.getElementById('table-b') );
I think you need to give us more details. Have you written the sorting mechanism?
我想你需要给我们更多细节。你有没有写过排序机制?