Skip to content

Commit

Permalink
Fix sortable to handle sorting of IP address columns properly. Resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-p committed Apr 3, 2010
1 parent 8b69a3e commit bef28e2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions usr/local/www/javascript/sorttable.js
Expand Up @@ -169,6 +169,9 @@ sorttable = {
for (var i=0; i<table.tBodies[0].rows.length; i++) {
text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
if (text != '') {
if (text.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) {
return sorttable.sort_ipaddr;
}
if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
return sorttable.sort_numeric;
}
Expand Down Expand Up @@ -298,6 +301,11 @@ sorttable = {
if (dt1<dt2) return -1;
return 1;
},
sort_ipaddr: function(a,b) {
if (ip2ulong(a[0]) == ip2ulong(b[0])) return 0;
if (ip2ulong(a[0]) < ip2ulong(b[0])) return -1;
return 1;
},

shaker_sort: function(list, comp_func) {
// A stable sort function to allow multi-level sorting of data
Expand Down Expand Up @@ -335,6 +343,19 @@ sorttable = {
Supporting functions: bundled here to avoid depending on a library
****************************************************************** */

function ip2ulong(ip) {
ip += "";
var ulip = false;
var octets = [];
if (ip.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) {
octets = ip.split('.');
for (i=0; i < 4; i++) {
ulip += octets[i] * Math.pow(256, (3-i));
}
}
return ulip;
}

// Dean Edwards/Matthias Miller/John Resig

/* for Mozilla/Opera9 */
Expand Down

0 comments on commit bef28e2

Please sign in to comment.