bug fix
[plewww.git] / plekit / tablesort / plc_customsort.js
1 /* $Id$ */
2 /*
3     sortAlphaNumeric{Top,Bottom}
4     -----------------------
5
6     These functions are derived from sortAlphaNumeric
7     inputs are expected to be alphaNumeric values e.g. 1, e, 1a, -23c, 54z
8
9     when comparing two values, the following happens
10     (*) if both have a numeric part, then they are compared; if equal the non-numeric part is used
11     (*) if only one has a numeric part, then 
12         . with sortAlphaNumericTop, the non-numeric value (typically n/a) happens first
13         . with sortAlphaNumericBottom, the non-numeric value (typically n/a) happens second
14 */
15 function _sortAlphaNumericPrepareData(tdNode, innerText){
16         var aa = innerText.toLowerCase().replace(" ", "");
17         var reg = /((\-|\+)?(\s+)?[0-9]+\.([0-9]+)?|(\-|\+)?(\s+)?(\.)?[0-9]+)([a-z]+)/;
18
19         if(reg.test(aa)) {
20                 var aaP = aa.match(reg);
21                 return [aaP[1], aaP[8]];
22         };
23
24         // Return an array
25         return isNaN(aa) ? ["",aa] : [aa,""];
26 }
27
28 /* non_numeric_first : 
29    when comparing, say, '12' with 'n/a':
30    if non_numeric_first is true, we return 1 (n/a greater than 12)
31    otherwise we return -1 
32 */
33 function _sortAlphaNumeric(a, b, non_numeric_first) {
34         // Get the previously prepared array
35         var aa = a[fdTableSort.pos];
36         var bb = b[fdTableSort.pos];
37
38         // Check numeric parts if not equal
39         if(aa[0] != bb[0]) {
40                 // both are numeric and have different numeric parts : usual comparison
41                 if(aa[0] != "" && bb[0] != "") { return aa[0] - bb[0]; };
42                 // one numeric value is missing
43                 if(aa[0] == "" && bb[0] != "") 
44                         return non_numeric_first ? -1 : 1 ;
45                 else 
46                         return non_numeric_first ? 1 : -1 ;
47         // from here and on, aa[0] == bb[0]
48         } else if (aa[1] == bb[1]) {
49           return 0;
50         // the alpha part differ
51         } else {
52           return (aa[1]<bb[1] ? -1 : 1);
53         }
54 }
55
56 function sortAlphaNumericBottomPrepareData(tdNode, innerText) {
57         return _sortAlphaNumericPrepareData(tdNode, innerText);
58 }
59 function sortAlphaNumericBottom(a,b) {
60         return _sortAlphaNumeric (a,b,false);
61 }
62 function sortAlphaNumericTopPrepareData(tdNode, innerText) {
63         return _sortAlphaNumericPrepareData(tdNode, innerText);
64 }
65 function sortAlphaNumericTop(a,b) {
66         return _sortAlphaNumeric (a,b,true);
67 }
68