[jQuery] basic sort function
Hi guys today I finally got around to using jQuery today and man do i
love it. wished that I had known about it before I started my last
project. would have saved me a couple of months work.
Ok well back to today
I needed to take a structure (table made of div's) and sort by
alphabetic order once I got my head around the way that jQuery worked my
boss who had used jQuery more showed me the exact code I needed for
doing the sort. what you have two css selector definitions, one for
the rows of your data and the other is the css selector information for
the element that holds the information that you want to sort on
basically what you would end up with is
$.sort("table.report tbody tr", "td.name a");
would sort the following table into alphabetic order
<table class='report'>
<thead>...</thead>
<tbody>
<tr><td class='name'><a href='zebra.html'>zebra</a></td><td>Looks
like a horse with stripes</td></tr>
<tr><td class='name'><a href='tiger.html'>tiger</a></td><td>Looks
like a horse with stripes but with sharp teeth.</td></tr>
</tbody>
</table>
I think that this would be a great addition to the jQuery base class or
should it just be a plugin.
$(document).ready(function(){
var rows = [];
$("table.report tbody tr").each(function(){
rows[rows.length] = [$(this).find("td.name a").html(),this];
});
rows.sort(function(a,b){
if (a[0] < b[0]) {
return -1
} else if (a[0] > b[0]) {
return 1
} else {
return 0;
}});
$.each(rows, function() {
this[1].parentNode.appendChild(this[1]);
});
});
I'll look into adding this to the jQuery-latest.js code later
Adrian Sweeney
___________________________________________________________
All New Yahoo! Mail Tired of Vi@gr@! come-ons? Let our SpamGuard protect you. http://uk.docs.yahoo.com/nowyoucan.html
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/