Jquery tinysort not work

时间:2016-08-31 17:45:18

标签: jquery html sorting tinysort

I am considering to use tinysort to sort a complex div list And Here is first try 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>

I wish that it will be

S2
S3
S5
S5-1
S5-2

However it's

S5
S5-2
S5-1
S2
S3

2 个答案:

答案 0 :(得分:1)

Make sure you have jquery included in your code, also include tinysort.defaults.ignoreDashes = true;. Refer the code snipet below

 $(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>

答案 1 :(得分:1)

You can use your own sorting function:

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>