TinySort will sort any nodetype by it’s text- or attribute value, or by that of one of it’s children.

You can download TinySort as zip or do a repository checkout. If you stumble upon anything out of the ordinary you can file them here.

The examples below should help getting you on your way.

usage

The first parse in the tsort function can either be nothing...

$("li").tsort();
$("ul#people>li").tsort();

a string (as you would in find)...

$("ul#people>li").tsort("span.surname");

a 'settings' object...

$("ul#people>li").tsort({place:"end"});

or both...

$("ul#people>li").tsort("img",{order:"desc",attr:"alt"});

Change default settings globally like so:

$.tinysort.defaults.order = "desc";
$.tinysort.defaults.attr = "title";

TinySort has four settings:

propertytypedescriptionpossible valuesdefault
order String the order of the sorting method
ascascending order
descdescending order
randrandom order
"asc"
attr String order by attribute value (ie title, href, class) anything ""
place String determines the placement of the ordered elements in respect to the unordered elements filtered out by using the 'attr' setting or the first parsed 'find' string
startsorted elements are prepended at the parent attribute
endsorted elements are appended at the parent attribute
orgplacement occurs at the positions the sortable elements are found
firstelements are appended after the first occurance of a sortable element
"start"
returns Boolean affects the returned jquery array
falseall elements are returned
trueonly sorted elements are returned
false

examples

default sorting

The default sort is done by simply calling the 'tsort' function onto your selection.

$("ul#xdflt>li").tsort();

sorted by sub-selection

You can provide an additional subselection by parsing a jquery sub-selection string into the tsort function. The returned array will be in the newly sorted order.
In this example the list elements are sorted to the text of the second span and a number is prepended.

$("ul#xsub>li").tsort("span:eq(1)").each(function(i){$(this).prepend(i+": ")});

sorted with attribute

You can provide an additional subselection by parsing a jquery sub-selection string into the tsort function. The following will only sort the non-striked elements.

$("ul#xattr>li").tsort("span[class!=striked]");

return only sorted elements

By default, all the elements are returned, even the ones excluded by your sub-selection. By parsing the additional parameter 'returns=true' only the sorted elements are returned.
You can also adjust the placement of the sorted values by adding the 'place' attribute. In this case the original positions are maintained.

$("ul#xret>li").tsort("span[class!=striked]",{returns:true,place:"org"}).css({color:"red"});

sorted by attribute value

Sort by attribute value by parsing the additional parameter 'orderby=attributeName'. This will sort by attribute of, either the jquery selection, or of the sub-selection (if provided).

$("ul#xval>li").tsort("a[title]",{orderby:"title"});

sorted descending

Sort by ascending or descending order by parsing the additional 'order="desc"/"asc"' parameter.

$("ul#xdesc>li").tsort("",{order:"desc"});

sort on any node

TinySort works on any nodeType. The following is a div with spans.

$("div#xany>span").tsort("",{order:"desc"});

images sorted by title

Sort by attribute title value.

$("span#ximg>img").tsort({attr:"title"});

sorted numbers

TinySort also works on numbers.

$("ul#xnum>li").tsort();

randomize

TinySort can also order randomly (or is that a contradiction).

$("ul#xrnd li").tsort("",{order:"rand"});

sorting tables

With a little extra code you can create a sortable table. The anchors in this table header call the function sortTable which basicly does this:

var aAsc = [];
function sortTable(nr) {
	aAsc[nr] = aAsc[nr]=="asc"?"desc":"asc";
	$("#xtable>tbody>tr").tsort("td:eq("+nr+")[abbr]",{order:aAsc[nr]});
}

Note that the mixed column only sorts those rows of which the td's have the abbr attribute set, and because of the default place value the non-sorted elements always remain at the bottom

word int float mixed mixed add row

known issues