用jquery排序表,没有其他插件

到目前为止,我已经得到了这段代码:

$('.link-sort-table').click(function(e) { var $sort = this; var $table = $('#sort-table'); var $rows = $('tbody > tr', $table); $rows.sort(function(a, b) { var keyA = $('td:eq(0)', a).text(); var keyB = $('td:eq(0)', b).text(); if ($($sort).hasClass('asc')) { return (keyA > keyB) ? 1 : -1; } else { return (keyA < keyB) ? 1 : -1; } }); $.each($rows, function(index, row) { $table.append(row); }); e.preventDefault(); }); 
 thead { color: green; } tbody { color: blue; } tfoot { color: red; } table, th, td { border: 1px solid black; } 
       
Sort: AZ ZA Filme Seriale Carti
1 2 3 4
Sprinter Cell The Martian My Name is Earl Harry Potter
Tomb Raider Inception The Big Bang Theory The Casual Vacancy
Need for Speer Ted Supergirl Fiddy shades
Counter Strike Limitless One-Punch Man Morometii
Half Life Pixels Fresh off the boat i KILL
FarCry James Bond House of Cards Punguta cu 2 bani
Dota Hunger Games DareDevil The Ink
The Adventures Of Van-Helsing Inception iZombie Blackout
The Hidden Silver Lining Playbook Game of Thrones Twilight
League of Legends Predestination Dexter The Imitation game

我需要通过按下标题来对我的列进行升序/降序排序,但我对jquery是新的,我只是想弄清楚,我无法找到一个很好的教程,只用jquery对表进行排序,我不允许使用任何其他插入

问题是你的细胞对于少数几个人的白色空间。 你可以.trim那些文本然后你没事:

  var keyA = $.trim($('td:eq(0)',a).text()); var keyB = $.trim($('td:eq(0)',b).text()); 

小提琴

使用jQuery.fn自己的函数并将其扩展为jQuery.fn

 var people = [ { 'myKey': 'Ankit', 'status': 0 }, { 'myKey': 'Bhavik', 'status': 3 }, { 'myKey': 'Parth', 'status': 7 }, { 'myKey': 'Amin', 'status': 9 }, { 'myKey': 'Russ', 'status': 9 }, { 'myKey': 'Pete', 'status': 10 }, { 'myKey': 'Ravi', 'status': 2 }, { 'myKey': 'Tejas', 'status': 2 }, { 'myKey': 'Dilip', 'status': 1 }, { 'myKey': 'Piyush', 'status': 12 } ]; alert("0. At start: " + JSON.stringify(people)); //META.fn: sortData jQuery.fn.sortData = function (prop, asc) { return this.sort(function (a, b) { var x = a[prop]; var y = b[prop]; var retrunStatus = ((x < y) ? 1 : ((x > y) ? -1 : 0)); return (asc==undefined || asc) ? (retrunStatus * -1) : retrunStatus ; }); } people2 = $(people).sortData('myKey',false); alert("1. After sorting (0 to x): " + JSON.stringify(people2));