2

我正在考虑使用tinysort对复杂div列表进行排序这里是第一次尝试html

<div id = "sort">
    <div id = "volume">
        <div id ="section1">S5
            <div>S5-2</div>
            <div>S5-1</div>
        </div>
        <div id="section2">S2</div>
        <div id="section3">S3</div>
    </div>
</div>
<script>
    $(document).ready(function() {
        tinysort('div#volume>div');
        tinysort('div#section1>div');
    });
</script>

我希望它会是

S2
S3
S5
S5-1
S5-2

然而它是

S5
S5-2
S5-1
S2
S3
4

2 回答 2

1

确保您的代码中包含 jquery,还包含tinysort.defaults.ignoreDashes = true;. 请参阅下面的代码片段

 $(document).ready(function() {
 	tinysort.defaults.ignoreDashes = true;
        tinysort('div#volume>div');
        tinysort('div#section1>div');
    });
<div id = "sort">
    <div id = "volume">
        <div id ="section1">S5
            <div>S5-2</div>
            <div>S5-1</div>
        </div>
        <div id="section2">S2</div>
        <div id="section3">S3</div>
    </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinysort/2.3.6/tinysort.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

于 2016-08-31T18:23:24.167 回答
1

您可以使用自己的排序功能:

function mySort(a, b) {
  return $(a.elm).text() < $(b.elm).text() ? -1 : 1;
}
$(document).ready(function() {
  tinysort('div#volume>div', {sortFunction: mySort });
  tinysort('div#section1>div', {sortFunction: mySort });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/tinysort/2.3.6/tinysort.min.js"></script>
<div id="sort">
    <div id="volume">
        <div id="section1">S5
            <div>S5-2</div>
            <div>S5-1</div>
        </div>
        <div id="section2">S2</div>
        <div id="section3">S3</div>
    </div>
</div>

于 2016-08-31T18:34:02.623 回答