//*** This code is copyright 2004 by Gavin Kistner, !@phrogz.net //*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt //*** Reuse or modification is free provided you abide by the terms of that license. //*** (Including the first two lines above in your source code mostly satisfies the conditions.) // Returns a function which can be used to sort an Array by multiple criteria. // Can be called in one of three ways: // var sortFunc = SortBy( 'name' , 'age' , 'sex' ); // var sortFunc = SortBy( ['name','age','sex'] ); // var sortFunc = SortBy( { name:1, age:1, sex:1 } ); // // The first two methods are equivalent, and sort in ascending order. // The last method allows you to specify sort direction: // 1 (or true) specifies ascending sort order. // 0 (or false) specifies descending sort order. // // The sort function is constructed using 'dot notation' for the properties; // this means you can also specify a method to call on the object by putting // parentheses at the end of the name. // e.g. var stupidDateStringSort = SortBy('toDateString()','toTimeString()'); function SortBy(){ var a=arguments, len=a.length, f='return ', p=1, i; if (len==1 && (a[0].constructor==Object || a[0].constructor==Array) && ((o=a=a[0]).constructor==Object)) p=0; if (p && (o={})) for (i=0,len=a.length;i']:['>','<']; f+='a.'+k+z[0]+'b.'+k+'?-1:a.'+k+z[1]+'b.'+k+'?1:'; } return new Function('a','b',f+'0'); } // Same as the function above, but uses .getAttribute() with the strings to find the value to sort by. // (Needed when using custom attributes of XML objects for browsers other than IE.) function SortByAttribute(){ var a=arguments, len=a.length, f='return ', p=1, i; if (len==1 && (a[0].constructor==Object || a[0].constructor==Array) && ((o=a=a[0]).constructor==Object)) p=0; if (p && (o={})) for (i=0,len=a.length;i']:['>','<']; f+='a.getAttribute("'+k+'")'+z[0]+'b.getAttribute("'+k+'")?-1:a.getAttribute("'+k+'")'+z[1]+'b.getAttribute("'+k+'")?1:'; } return new Function('a','b',f+'0'); } /********************************************************************************* *** EXAMPLES ********************************************************************* var people = []; people.push( { name:'Bob' , age:15 , sex:'M' } ); people.push( { name:'Carol' , age:18 , sex:'F' } ); people.push( { name:'Sue' , age:33 , sex:'F' } ); people.push( { name:'Angie' , age:8 , sex:'F' } ); people.push( { name:'David' , age:12 , sex:'M' } ); var sortByName = SortBy('name'); people.sort( sortByName ); // people is now: // [ // {name:'Angie',age:8,sex:'F'}, // {name:'Bob',age:15,sex:'M'}, // {name:'Carol',age:18,sex:'F'}, // {name:'David',age:12,sex:'M'}, // {name:'Sue',age:33,sex:'F'} // ] var sortBySexThenAge = SortBy('sex','age'); people.sort( sortBySexThenAge ); // people is now: // [ // {name:'Angie',age:8,sex:'F'}, // {name:'Carol',age:18,sex:'F'}, // {name:'Sue',age:33,sex:'F'}, // {name:'David',age:12,sex:'M'}, // {name:'Bob',age:15,sex:'M'} // ] people.sort( SortBy({sex:0,name:0}) ); // people is now: // [ // {name:'David',age:12,sex:'M'}, // {name:'Bob',age:15,sex:'M'}, // {name:'Sue',age:33,sex:'F'}, // {name:'Carol',age:18,sex:'F'}, // {name:'Angie',age:8,sex:'F'} // ] people.sort( SortBy({name:0}) ); // people is now: // [ // {name:'Sue',age:33,sex:'F'}, // {name:'David',age:12,sex:'M'}, // {name:'Carol',age:18,sex:'F'}, // {name:'Bob',age:15,sex:'M'}, // {name:'Angie',age:8,sex:'F'} // ] *********************************************************************************/