1/* 2 * allows to sort objects by multiple properties and determine if you want the field parse first (i.e. parse int) 3 * or if you want to reverse the sorting direction of the property 4 */ 5var sort_by; 6 7(function() { 8 // utility functions 9 var default_cmp = function(a, b) { 10 if (a == b) return 0; 11 return a < b ? -1 : 1; 12 }, 13 getCmpFunc = function(primer, reverse) { 14 var dfc = default_cmp, // closer in scope 15 cmp = default_cmp; 16 if (primer) { 17 cmp = function(a, b) { 18 return dfc(primer(a), primer(b)); 19 }; 20 } 21 if (reverse) { 22 return function(a, b) { 23 return -1 * cmp(a, b); 24 }; 25 } 26 return cmp; 27 }; 28 29 // actual implementation 30 sort_by = function() { 31 var fields = [], 32 n_fields = arguments.length, 33 field, name, reverse, cmp; 34 35 // preprocess sorting options 36 for (var i = 0; i < n_fields; i++) { 37 field = arguments[i]; 38 if (typeof field === 'string') { 39 name = field; 40 cmp = default_cmp; 41 } 42 else { 43 name = field.name; 44 cmp = getCmpFunc(field.primer, field.reverse); 45 } 46 fields.push({ 47 name: name, 48 cmp: cmp 49 }); 50 } 51 52 // final comparison function 53 return function(A, B) { 54 var a, b, name, result; 55 for (var i = 0; i < n_fields; i++) { 56 result = 0; 57 field = fields[i]; 58 name = field.name; 59 60 result = field.cmp(A[name], B[name]); 61 if (result !== 0) break; 62 } 63 return result; 64 } 65 } 66}()); 67 68 69/* 70 * this functions allow sorting alphanumeric arrays (i.e. [1, A10, B, 3, 10, 7, A7] would become [1, 3, 7, 10, A7, A10, B]) 71 * it does not have proper handling for values that are numeric first then alphabetic i.e. (1B, 1C) 72**/ 73function sortAlphaNum(a,b,reverse) { 74 var reA = /[^a-zA-Z]/g; //get everything that's not a letter 75 var reN = /[^0-9]/g; //get everything that's not a number 76 var AInt = parseInt(a, 10); 77 var BInt = parseInt(b, 10); 78 console.log(a, b); 79 80 if(isNaN(AInt) && isNaN(BInt)){ 81 var aA = a.replace(reA, ""); //replace everything that isn't a letter with nothing, leaving you with just letters 82 var bA = b.replace(reA, ""); //replace everything that isn't a letter with nothing, leaving you with just letters 83 if(aA === bA) { 84 var aN = parseInt(a.replace(reN, ""), 10); //replace everything that isn't a number with nothing, leaving you with just numbers 85 var bN = parseInt(b.replace(reN, ""), 10); //replace everything that isn't a number with nothing, leaving you with just numbers 86 return aN === bN ? 0 : aN > bN ? 1 : -1; 87 } else { 88 return aA > bA ? 1 : -1; 89 } 90 }else if(isNaN(AInt)){//A is not an Int 91 return (reverse ? -1 : 1);//1 sorts alphabetic prefaced ones last, -1 sorts numeric values first 92 }else if(isNaN(BInt)){//B is not an Int 93 return (reverse ? 1 : -1);//-1 sorts alphabetic prefaced ones last, 1 sorts numeric values first 94 }else{ 95 return AInt > BInt ? 1 : -1; 96 } 97} 98 99//this function allows you to add x number of 0's on to the front of any string 100function pad (str, max) { 101 str = str.toString(); 102 return str.length < max ? pad("0" + str, max) : str; 103} 104 105//this function allows comparing the elements in 2 arrays 106function compare2arrays(a, b, index) 107{ 108 if (typeof index === "undefined") 109 { 110 index = 0; 111 } 112 113 if (index >= a.length || index >= b.length) 114 { 115 return 0; 116 } 117 118 if (a[index] > b[index]) 119 { 120 return 1; 121 } 122 else if(a[index] < b[index]) 123 { 124 return -1; 125 } 126 else 127 { 128 return compare2arrays(a, b, index + 1); 129 } 130}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.