Sort IP addresses properly
authorJamie Cameron <jcameron@webmin.com>
Mon, 17 Oct 2011 19:35:52 +0000 (12:35 -0700)
committerJamie Cameron <jcameron@webmin.com>
Mon, 17 Oct 2011 19:35:52 +0000 (12:35 -0700)
https://sourceforge.net/tracker/index.php?func=detail&aid=3424704&group_id=17457&atid=117457#

blue-theme/unauthenticated/sorttable.js

index 624272d..d14cb41 100644 (file)
@@ -81,6 +81,7 @@ function ts_resortTable(lnk,clid) {
     // Special cases for our mailbox lists
     if (itm.match(/^(Empty|Unlimited)$/)) sortfn = ts_sort_filesize;
     if (itm.match(/^[\d\.]+%?$/)) sortfn = ts_sort_numeric;
+    if (itm.match(/^\d+\.\d+\.\d+\.\d+$/)) sortfn = ts_sort_ip;
     SORT_COLUMN_INDEX = column;
     var firstRow = new Array();
     var newRows = new Array();
@@ -240,6 +241,24 @@ function ts_sort_caseinsensitive(a,b) {
     return 1;
 }
 
+function ts_sort_ip(a,b) {
+    aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
+    bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
+    var regexp = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
+    var matchA = aa.match(regexp);
+    var matchB = bb.match(regexp);
+    return !matchA ? -1 :
+          !matchB ? 1 :
+          parseInt(matchA[1]) < parseInt(matchB[1]) ? -1 :
+           parseInt(matchA[1]) > parseInt(matchB[1]) ? 1 :
+          parseInt(matchA[2]) < parseInt(matchB[2]) ? -1 :
+           parseInt(matchA[2]) > parseInt(matchB[2]) ? 1 :
+          parseInt(matchA[3]) < parseInt(matchB[3]) ? -1 :
+           parseInt(matchA[3]) > parseInt(matchB[3]) ? 1 :
+          parseInt(matchA[4]) < parseInt(matchB[4]) ? -1 :
+           parseInt(matchA[4]) > parseInt(matchB[4]) ? 1 : 0;
+}
+
 function ts_sort_default(a,b) {
     aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
     bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);