How to sort two tables side by side so that changes in one are reflected in the other

Suppose I have two tables A and B, each of which has only 1 column.

Each row in table A corresponds to each row in B, but I do not want them in the same table.

A       B 
------  ----------
car     automobile
bike    train

When I sort alphabetically, I have to get

A       B 
------  ----------
bike    train
car     automobile
+3
source share
2 answers

Since the elements of table A have a one-to-one correspondence with the elements of table B, it is better to use an object to represent this data. An array of these objects represents the entire data set. Whenever sorting is required, just sort this array and write down the tables.

// first represents the item in table A
// second represents the item in table B
function Transporter(first, second) {
    this.first = first;
    this.second = second;
}

, first Transporter.

function compare(a, b) {
    if(a.first < b.first) {
      return -1;
    }
    else if(a.first > b.first) {
      return 1;
    }
    return 0;
}

:

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)

, .

, , script . jQuery: http://tablesorter.com/docs/

+1

, , , .

sortingModule.sort( document.getElementById('table-a') );
sortingModule.sort( document.getElementById('table-b') );

, . ?

+1

Source: https://habr.com/ru/post/1730702/


All Articles