/* * jQuery TinySort 1.3.27 * A plugin to sort child nodes by (sub) contents or attributes. * * Copyright (c) 2008-2012 Ron Valstar http://www.sjeiti.com/ * * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * contributors: * brian.gibson@gmail.com * michael.thornberry@gmail.com * * Usage: * $("ul#people>li").tsort(); * $("ul#people>li").tsort("span.surname"); * $("ul#people>li").tsort("span.surname",{order:"desc"}); * $("ul#people>li").tsort({place:"end"}); * * Change default like so: * $.tinysort.defaults.order = "desc"; * * in this update: * - replaced pushStack with actual replace so initial jQ object is reordered (not only the returned object) * - fixed non-latin character ordering * * in last update: * - removed isNum * - fixed mixed literal/numeral values * - refactored fn contains() * - revision number now corresponds to svn revision * * Todos: * - todo: uppercase vs lowercase * - todo: 'foobar' != 'foobars' in non-latin * */ ;(function($) { // private vars var fls = !1 // minify placeholder ,nll = null // minify placeholder ,prsflt = parseFloat // minify placeholder ,frCrCd = String.fromCharCode // minify placeholder ,mathmn = Math.min // minify placeholder ,rxLastNr = /(-?\d+\.?\d*)$/g // regex for testing strings ending on numbers // // character specific ordering is off by default for speed ,sCharOrder // equals the input oSettings.charOrder so we can test any changes ,aAllChars = [] // all latin chars 32-255 ,aOrderChar // similar to sAllChars but with the changed char order ,bDoubles // boolean indicating double-non-latin chars, ie: lj, dž, Aa, ch, ss etc... ,iReplace = 0x2500 // doubles are replaced with Unicode char starting at 0x2500 ,oReplace = {} // replacement object ,rxNotLatin // regular expression to test for non-latin chars ; // fix IE8 indexOf (issue 26) if (!Array.indexOf) { Array.prototype.indexOf = function (o) { for (var i=0,l=this.length;i96&&iChar<123; // 'a'.charCodeAt()===97 'z'.charCodeAt()===122 if (!bIsLatin){ if (sChar=='[') { // find replace chars: ë will sort similar to e var iCharNotLatin = aCharNotLatin.length ,sLastChar = iCharNotLatin?aCharNotLatin[iCharNotLatin-1]:sCharLatin ,sReplaces = sCharOrder.substr(i+1).match(/[^\]]*/)[0] ,aDoubles = sReplaces.match(/{[^}]*}/g); // find doubles: dž, ss, lj ... if (aDoubles) { for (j=0,m=aDoubles.length;jsB?1:0)); // test for non latin chars if (!bNumeric&&oSettings.charOrder) { if (bDoubles) { // first replace doubles for (var s in oReplace) { var o = oReplace[s]; sA = sA.replace(s,o); sB = sB.replace(s,o); } } // then test if either word has non latin chars // we're using the slower string.match because strangely regex.test sometimes fails if (sA.match(rxNotLatin)!==nll||sB.match(rxNotLatin)!==nll) { for (var k=0,l=mathmn(sA.length,sB.length);kiBchr?1:0))) break; } } } return iReturn; }; oThis.each(function(i,el) { var $Elm = $(el) // element or sub selection ,mElmOrSub = bFind?(bFilter?$Filter.filter(el):$Elm.find(_find)):$Elm // text or attribute value ,sSort = bData?''+mElmOrSub.data(oSettings.data):(bAttr?mElmOrSub.attr(oSettings.attr):(oSettings.useVal?mElmOrSub.val():mElmOrSub.text())) // to sort or not to sort ,mParent = $Elm.parent(); if (!oElements[mParent]) oElements[mParent] = {s:[],n:[]}; // s: sort, n: not sort if (mElmOrSub.length>0) oElements[mParent].s.push({s:sSort,e:$Elm,n:i}); // s:string, e:element, n:number else oElements[mParent].n.push({e:$Elm,n:i}); }); // // sort for (sParent in oElements) oElements[sParent].s.sort(fnSort); // // order elements and fill new order for (sParent in oElements) { var oParent = oElements[sParent] ,aOrg = [] // list for original position ,iLow = iLen ,aCnt = [0,0] // count how much we've sorted for retreival from either the sort list or the non-sort list (oParent.s/oParent.n) ,i; switch (oSettings.place) { case 'first': $.each(oParent.s,function(i,obj) { iLow = mathmn(iLow,obj.n) }); break; case 'org': $.each(oParent.s,function(i,obj) { aOrg.push(obj.n) }); break; case 'end': iLow = oParent.n.length; break; default: iLow = 0; } for (i = 0;i=iLow&&i