简体   繁体   English

如何在单个表格行中对值进行排序? (即水平排序)

[英]How do I sort the values in a single table row? (i.e. sort horizontally)

The question is simple but for some reason, wasn't so simple for me finding an answer. 这个问题很简单,但是由于某种原因,对于我来说找到答案并不是那么简单。

All I want to do is sort the cells of a table row - for lack of a better description - 'horizontally', not 'vertically'. 我只想对表格行的单元格进行排序-缺乏更好的描述-“水平”而不是“垂直”。 Everything I've found so far deals with sorting individual columns or sorting based on column headers etc. 到目前为止,我发现的所有内容都涉及对单个列进行排序或基于列标题进行排序等。

There are no headers, just a simple table such as this. 没有标题,只有一个像这样的简单表。

<table id="t1">
    <tr>
        <td>Sample-N</td>
        <td>Sample-W</td>
        <td>Sample-A</td>
        <td>Sample-K</td>
    </tr>
    <tr>
        <td>Sample-S</td>
        <td>Sample-U</td>
        <td>Sample-J</td>
        <td>Sample-M</td>
    </tr>
    <tr>
        <td>Sample-O</td>
        <td>Sample-E</td>
        <td>Sample-L</td>
        <td>Sample-B</td>
    </tr>
</table>

Then after sorting, it should appear like this: 然后排序后,它应如下所示:

<table id="t1">
    <tr>
        <td>Sample-A</td>
        <td>Sample-K</td>
        <td>Sample-N</td>
        <td>Sample-W</td>
    </tr>
    <tr>
        <td>Sample-J</td>
        <td>Sample-M</td>
        <td>Sample-S</td>
        <td>Sample-U</td>
    </tr>
    <tr>
        <td>Sample-B</td>
        <td>Sample-E</td>
        <td>Sample-L</td>
        <td>Sample-O</td>
    </tr>
</table>

If an answer already exists (preferably using jQuery), can someone please point me in the right direction? 如果已经存在答案(最好使用jQuery),有人可以指出正确的方向吗? Because I can't find it. 因为我找不到。

Even the tablesorter plug-in only seems to sort individual columns. 甚至tablesorter插件似乎也只能对各个列进行排序。

You can make an array of the elements, sort them with a custom sorting method (using String.localeCompare to compare the text contents) then append the items back into their parent in the correct order. 您可以创建一个元素数组,使用自定义排序方法对它们进行排序(使用String.localeCompare来比较文本内容),然后以正确的顺序将各项附加回其父项。

 $('#t1 tr').each(function(){ var $tr = $(this), $tds = $tr.children(), tdArray = $.makeArray($tds); tdArray.sort(function(a, b){ return $(a).text().localeCompare($(b).text()); }); $.each(tdArray,function(i, el){ $(el).appendTo($tr); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="t1"> <tr> <td>Sample-N</td> <td>Sample-W</td> <td>Sample-A</td> <td>Sample-K</td> </tr> <tr> <td>Sample-S</td> <td>Sample-U</td> <td>Sample-J</td> <td>Sample-M</td> </tr> <tr> <td>Sample-O</td> <td>Sample-E</td> <td>Sample-L</td> <td>Sample-B</td> </tr> </table> 


As @Karl-André Gagnon points out, this can be shortened to just 正如@Karl-AndréGagnon指出的那样,可以将其缩短为

$('#t1 tr').each(function(){
    $(this).children().sort(function(a, b){
        return $(a).text().localeCompare($(b).text());
    }).appendTo(this);
});

because jQuery returns an array-like object from .children() that contains the native .sort() method of a regular array. 因为jQuery从.children()返回一个类似数组的对象,该对象包含常规数组的本机.sort()方法。 I would personally not use this version due to relying on that method being included in the returned object, and instead go with the $.makeArray() solution that creates a native array which is sure to contain the sort method. 我个人不会使用此版本,因为依赖于该方法包含在返回的对象中,而是使用$.makeArray()解决方案来创建一个本机数组,该数组肯定包含sort方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM