Skip to content

Commit

Permalink
Complete sortableTable.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 2, 2015
1 parent e8130fe commit 686331e
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions sortableTable/sortableTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,27 @@ $(function () {
}
};

var sort = function (array) {
return Array.prototype.sort.call(array, function (a, b) {
return b[0] - a[0];
});
};

var $tr = $('tr');
var $tbody = $('tbody');
var $tr = $('tbody > tr');
var order = false;

$('th').each(function (thIdx, thElement) {
$(thElement).on('click', function () {
var result = [];
var order = [];

for (var i = 1; i < $tr.length; i += 1) {
result.push( [ convert(thIdx, $tr[i].children[thIdx].innerHTML), i] );
order = !order;

if (order) {
$tr.sort(function (a, b) {
return convert(thIdx, b.children[thIdx].innerHTML) > convert(thIdx, a.children[thIdx].innerHTML);
});
} else {
$tr.sort(function (a, b) {
return convert(thIdx, b.children[thIdx].innerHTML) < convert(thIdx, a.children[thIdx].innerHTML);
});
}

sort(result);

result.forEach(function (array) {
order.push( array[1] );
$.each($tr, function (index, row) {
$tbody.append(row);
});

console.log( order );
});
});
});
Expand Down

0 comments on commit 686331e

Please sign in to comment.