Skip to content Skip to sidebar Skip to footer

How To Sort A List Of Objects In Jsp

I have a list of items that will be shown to user. I know to sort the items I can send the list to back-end, sort them and show them again but how to do it in front-end so no need

Solution 1:

You can sort the elements, take a look the example I did to sort by number:

http://jsfiddle.net/tnnqyxcw/1/

js:

$(document).ready(function(){
    $('button').on('click', function(){
        var s = $(this).data('sort'); console.log(s);
        if(s === 0){
            $(this).data('sort', 1);
            $('.clist div').sort(function(a,b){
               return a.dataset.sid < b.dataset.sid
            }).appendTo('.clist')            
        }else{
            $(this).data('sort', 0);
            $('.clist div').sort(function(a,b){
               return a.dataset.sid > b.dataset.sid
            }).appendTo('.clist')
        }
    });
});

Html

<buttondata-sort="0">Sort:</button><br><divclass="clist"><divdata-sid=1>Number 1</div><divdata-sid=4>Number 4</div><divdata-sid=3>Number 3</div><divdata-sid=1>Number 1</div><divdata-sid=4>Number 4</div><divdata-sid=2>Number 2</div><divdata-sid=1>Number 1</div></div>

So you can use data- to sort for all you want and then assign the click to the button or whatever you want.

I hope it's help.

Post a Comment for "How To Sort A List Of Objects In Jsp"