User:PanSola/sortable mod.js/sort types

/* * This is a "nightly" version. * For stable version, see MediaWiki:sortable_mod.js/abstractions */ /*

Sort Type Abstractions
*/

function sortType_cons( name, guessFn, keyFn, sorter){ return new Array( name, guessFn, keyFn, sorter ); }

function sortType_getName (sortType) { return sortType[0]; }

function sortType_getGuessFn (sortType) { return sortType[1]; }

function sortType_getKeyFn (sortType){ return sortType[2]; }

function sortType_getSorter (sortType) { return sortType[3]; }

/*

Standard sorting functions
*/

/* Pairs of functions that sorts in different directions. */ function makeSorterPairs (myFunction) { return cons(myFunction, inv(myFunction)); }

function inv (fn) { return function (a, b) { return fn(b,a) } }

/*

ts_sort_default
*/ var ts_sort_default = makeSorterPairs(function (a,b) {	aa = rr_getSortKey(a);	bb = rr_getSortKey(b);	if (aa==bb) return 0;	if (aa<bb) return -1;	return 1; })

/*

Standard Types
*/

/*

case_insensitive and numeric
*/

var sortType_case_insensitive = sortType_cons('case_insensitive', 	function(itm){return true;}, 	function (a) {return a.toLowerCase;},	ts_sort_default);

var sortType_numeric = sortType_cons('numeric',	function(itm){		return (itm.match(/^[\d\.\,\-\+]+\%?$/) || itm.match(/^[\d\.\,\-\+]+[eE][\d\-\+]+\%?$/) || itm.match(/^[\d\.\,\-\+]+e[\d\-\+]+\u00d710[\d\-\+]+\%?$/) ) },	key_numeric,	ts_sort_default);

function key_numeric (a) { if (!a) a=0; a = parseFloat(a); a = (isNaN(a) ? 0 : a); return a; } /*

convert_numeric(a)
*/ function convert_numeric(a){ return a.replace(/¼/, ".25").replace(/½/, ".5").replace(/¾/, ".75").replace(/,/, ""); } /*

date
*/

var sortType_date = sortType_cons('date',	function(itm){		return (itm.match(/^\d\d[\/. -][a-zA-Z]{3}[\/. -]\d\d\d\d$/) || itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d\d\d$/) || itm.match(/^\d\d[\/.-]\d\d[\/.-]\d\d$/) ) },	key_date,	ts_sort_default);

function key_date(date) { // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX dt = "00000000"; if (date.length == 11) { monthstr = date.substr(3,3); monthstr = monthstr.toLowerCase; switch(monthstr) { case "jan": var month = "01"; break; case "feb": var month = "02"; break; case "mar": var month = "03"; break; case "apr": var month = "04"; break; case "may": var month = "05"; break; case "jun": var month = "06"; break; case "jul": var month = "07"; break; case "aug": var month = "08"; break; case "sep": var month = "09"; break; case "oct": var month = "10"; break; case "nov": var month = "11"; break; case "dec": var month = "12"; break; // default: var month = "00"; }		dt = date.substr(7,4)+month+date.substr(0,2); return dt; } else if (date.length == 10) { if (europeandate == false) { dt = date.substr(6,4)+date.substr(0,2)+date.substr(3,2); return dt; } else { dt = date.substr(6,4)+date.substr(3,2)+date.substr(0,2); return dt; }	} else if (date.length == 8) { yr = date.substr(6,2); if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; }		if (europeandate == true) { dt = yr+date.substr(3,2)+date.substr(0,2); return dt; } else { dt = yr+date.substr(0,2)+date.substr(3,2); return dt; }	}	return dt; }

/*

unit_num & num_unit
*/

var ts_sort_unit_num = makeSorterPairs(function (a,b) {	aa = rr_getSortKey(a)	bb = rr_getSortKey(b)	if (aa[0] == bb[0]) return (aa[1]-bb[1]);	if (aa[0] < bb[0]) return -1;	return 1; })

var sortType_unit_num = sortType_cons('unit_num',	function (itm) {		return itm.match(/^[^\d.,¼½¾]+[\d.,]+\%?$/)},	key_unit_num,	ts_sort_unit_num);

var sortType_num_unit = sortType_cons('num_unit',	function (itm) {		return itm.match(/^[\d.,¼½¾]+\%?[^\d.,%]+$/)},	key_num_unit,	ts_sort_unit_num);

function key_unit_num(a) { var aa_idx = a.search(/[\d.¼½¾]/); var aa_unit = a.substr(0,aa_idx); var aa_num = convert_numeric(a.substr(aa_idx)); return [key_unit(aa_unit), key_numeric(aa_num)]; }

function key_num_unit(a) { var aa_idx = a.search(/[^\d.¼½¾]/); var aa_num = key_numeric(convert_numeric(a.substr(0,aa_idx))); var aa_unit = key_unit(a.substr(aa_idx)); //debug('Parsing "'+a+'" as "'+aa_unit+'" and "'+aa_num+'"'); return [aa_unit, aa_num]; }

function key_unit(unit){ return unit.toLowerCase; } /*

Custom Types
*/

/*

Currency
*/

var sortType_currency = sortType_cons('currency',	function(itm){ return itm.match(/^[\u00a3$\u20ac\u00a5]/)}, // pound dollar euro yen	key_unit_num,	ts_sort_unit_num);

/*

Registering sort types
*/

// This should be a list of all sort types. Sort types not listed here cannot be manually specified by the "axis=" attribute. Ordering only minorly affects performance (if you care, try to put the more common sort types in the front). var manualSortTypes = new Array (sortType_num_unit, sortType_numeric, sortType_case_insensitive, sortType_unit_num,  sortType_currency, sortType_date);

// This is the order in which sort type will try to auto-detect. If a string can be ambiguously detected to be of multiple sort types, the first match in the array will be used, so be careful with ordering (or regexp specification). var autoDetectSortTypes = new Array (sortType_numeric, sortType_currency, sortType_date, sortType_unit_num, sortType_num_unit, sortType_case_insensitive);

/*