Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort list based on data attribute (using jquery metadata plugin)

I'm having a tough time figuring out how to go about sorting a list based on one of the key/value pairs in the data attribute.

<ul>
    <li data="{name:'Foo', number: '1'}">
        <h3>Foo</h3>
    </li>

    <li data="{name:'Bar', number: '2'}">
        <h3>Bar</h3>
    </li>
</ul>

Here's the jQuery. I'm setting the metadata attr and accessing it. All of this works fine.

jQuery.metadata.setType("attr", "data");

$(document).ready(function(){   

    // loop through each list item and get the metadata
    $('ul li').each(function () {  
        console.log($(this).metadata());
        // shows the following - Object {name="Foo", number="1"}
    });

)};

Example: I would need to sort by Name ascending. Can someone point me in the right direction?

EDIT:

Here's the form I'm using to trigger the sorting:

<form name="filterForm">
    Sort: 
    <select name="sort" size="1">
        <option value="nameAsc" <cfif URL.sort EQ 1>selected="selected"</cfif>>Name A to Z</option>
        <option value="nameDesc" <cfif URL.sort EQ 2>selected="selected"</cfif>>Name Z to A</option>
        <option value="numberAsc" <cfif URL.sort EQ 3>selected="selected"</cfif>>Number 1-10</option>
        <option value="numberDesc" <cfif URL.sort EQ 4>selected="selected"</cfif>>Number 10-1</option>
    </select> 
</form> 

And the jquery to handle the change event, but I'm not sure I implemented Mikael Eliasson's code correctly because I'm not sure where to pass the value of what was selected:

$('form[name=filterForm] select').change(function(){

    var sortBy = this.value;

    var arr = []

    // loop through each list item and get the metadata
        $('ul li').each(function () {  
            var meta = $(this).metadata();
            meta.elem = $(this);
            arr.push(meta);
        });

    arr.sort(appendItems());

    function appendItems(){

        //Foreach item append it to the container. The first i arr will then end up in the top
        $.each(arr, function(index, item){
            item.elem.appendTo(item.elem.parent());
        }       

    }


});
like image 534
jyoseph Avatar asked Nov 23 '10 17:11

jyoseph


1 Answers

Javascript has a sort function which can take a function as argument. This function is supposed to be used when you have custom comparisons. See http://www.w3schools.com/jsref/jsref_sort.asp

So what I would do is somethings like this

var arr = []
// loop through each list item and get the metadata
    $('ul li').each(function () {  
        var meta = $(this).metadata();
        meta.elem = $(this);
        arr.push(meta);
    });
 arr.sort(compare);

//Foreach item append it to the container. The first i arr will then end up in the top
$.each(arr, function(index, item){
    item.elem.appendTo(item.elem.parent());
});

EDIT: Updated syntax on above .each loop - fixing syntax error

EDIT: Corrected a error in the last each loop

EDIT2: By request I'm updating with the compare function

function compare(a, b){
   return a.name - b.name;
}
like image 104
Mikael Eliasson Avatar answered Nov 10 '22 12:11

Mikael Eliasson