1 /** 2 * @fileOverview Array Sorting functionality 3 * @author <a href="http://www.bobs-bits.com">Stephen Reay</a> 4 */ 5 6 /** 7 * @namespace Common sorting methods 8 */ 9 BTM.Sort = {}; 10 11 /** 12 * Currency symbols ignored when using {@link BTM.Sort.currency} 13 * @todo Sort between major/minor (e.g. dollars/cents) to allow better sorting 14 * @see {BTM.Sort.CurrencySymbolRegExp} 15 */ 16 BTM.Sort.currencySymbols = [ 17 "\\$", 18 //Cent 19 "¢", 20 "¢", 21 "\\u00A2", 22 //Pound 23 "£", 24 "£", 25 "\\u00A3", 26 //Currency 27 "¤", 28 "¤", 29 "\\u00A4", 30 //Yen 31 "¥", 32 "¥", 33 "\\u00A5", 34 //Euro 35 "€", 36 "€", 37 "\\u20AC" 38 ]; 39 40 /** 41 * Get the currency symbols defined in {@link BTM.Sort.currencySymbols} as a RegExp object to use with search & replace 42 * @returns {RegExp} a RegExp object to match any of the currency symbols defined in {@link BTM.Sort.currencySymbols} 43 * @see BTM.Sort.currencySymbols 44 */ 45 BTM.Sort.currencySymbolsRegExp = function currencySymbolsRegExp() { 46 return new RegExp(BTM.Sort.currencySymbols.join('|'),'g'); 47 }; 48 49 /** 50 * Worker Function for Sorting 51 * @private 52 * @param a the first element to compare 53 * @param b the second element to compare 54 * @returns {Number} -1, 0 or 1, depending on how the elements compare 55 */ 56 BTM.Sort.sort = function sort(a, b) { 57 if (a < b) { 58 return -1; 59 } 60 if (a > b) { 61 return 1; 62 } 63 return 0; 64 }; 65 66 /** 67 * General text-sorting function 68 * @param {String} a the first of the two elements to compare 69 * @param {String} b the second of the two elements to compare 70 * @returns {Number} either -1, 0 or 1, depending on how the two elements compare to each other 71 */ 72 BTM.Sort.general = function general(a, b) { 73 return BTM.Sort.sort(a.stripTags().toLowerCase()+"", b.stripTags().toLowerCase()+""); 74 }; 75 76 /** 77 * Case-sensitive text-sorting function 78 * @param {String} a the first of the two elements to compare 79 * @param {String} b the second of the two elements to compare 80 * @returns {Number} either -1, 0 or 1, depending on how the two elements compare to each other 81 */ 82 BTM.Sort.caseSensitive = function caseSensitive(a, b) { 83 return BTM.Sort.sort(a.stripTags()+"", b.stripTags()+""); 84 }; 85 86 /** 87 * Numeric sorting function 88 * @param {String} a the first of the two elements to compare 89 * @param {String} b the second of the two elements to compare 90 * @returns {Number} either -1, 0 or 1, depending on how the two elements compare to each other 91 */ 92 BTM.Sort.numeric = function numeric(a, b) { 93 return BTM.Sort.sort(parseFloat(a), parseFloat(b)); 94 }; 95 96 /** 97 * Currency sorting function 98 * @param {String} a the first of the two elements to compare 99 * @param {String} b the second of the two elements to compare 100 * @returns {Number} either -1, 0 or 1, depending on how the two elements compare to each other 101 */ 102 BTM.Sort.currency = function currency(a, b) { 103 return numeric(a.replace(BTM.Sort.currencySymbolsRegExp(),""), b.replace(BTM.Sort.currencySymbolsRegExp(),"")); 104 }; 105 106 /** 107 * Document-Order sorting function 108 * @param {String} a the first of the two elements to compare 109 * @param {String} b the second of the two elements to compare 110 * @returns {Number} either -1, 0 or 1, depending on how the two elements compare to each other 111 */ 112 BTM.Sort.documentOrder = function documentOrder(a, b) { 113 if (BTM.$(a).sourceIndex) { 114 return BTM.Sort.sort(BTM.$(a).sourceIndex, BTM.$(b).sourceIndex); 115 } 116 else if (BTM.$(a).compareDocumentPosition) { 117 return 3 - (BTM.$(a).compareDocumentPosition(BTM.$(b)) & 6); 118 } 119 };