From 5470f7a42dfd3307cd204ab42c315b729267b186 Mon Sep 17 00:00:00 2001 From: Paik Paustian Date: Wed, 22 Dec 2021 14:27:55 +0100 Subject: [PATCH 1/2] Fix IP address sorting when there are empty entries in the table When a table contains empty values, sorting by IP addresses using the `ip-address.js` sorting plugin caused erratic sorting behaviour. The empty lines are not sorted correctly and remain scattered about the rest of the rows. This seems to be due to a type issue: The ip-address sorting plugin has a guard-clause if-statement that checks if the value that is to be sorted is false-y. In that case, the integer `0` is returned. The function seems to usually normalise ip addresses to a lexicographically sortable format, and values like `"86b4b93fbbf0418144b55bc45057b720"` and `"173040030054"` are returned. The resulting comparison between strings and integers seems to cause misplaced empty rows and all-over weird sorting behaviour. This commit changes the value returned by the guard-clause to the string value `"000000000000"`, which is simliar to the normalized IPv4 format already used by the plugin. --- sorting/ip-address.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/ip-address.js b/sorting/ip-address.js index c06f9f6..4441b35 100644 --- a/sorting/ip-address.js +++ b/sorting/ip-address.js @@ -25,7 +25,7 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, { var x, xa; if (!a) { - return 0 + return '000000000000'; } a = a.replace(/<[\s\S]*?>/g, ""); From 0688007640cd8d9b9485281ef84693b06092c379 Mon Sep 17 00:00:00 2001 From: Kevin Gilkey-Graham Date: Wed, 29 Dec 2021 14:36:27 -0500 Subject: [PATCH 2/2] Added port to IPV4 sorting and reduced if/else chains The original method of extracting the IP address from an IP:PORT pair never re-applied the PORT number to the sorting algorithm. This commit has added the functionality for the port number to be included in the sorting for IPv4 if it existed in the original value by appending it to the final zero-filled IPv4 address string. This commit also reduces the number of if/else chains to perform the zerofill by using a substr of a zeroed pattern matching the desired size of the field. "000" for IPv4 octets, "0000" for IPv6, and "00000" for port numbers. --- sorting/ip-address.js | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/sorting/ip-address.js b/sorting/ip-address.js index 4441b35..819bf07 100644 --- a/sorting/ip-address.js +++ b/sorting/ip-address.js @@ -8,6 +8,7 @@ * @author Dominique Fournier * @author Brad Wasson * @author Peter Vilhan + * @author Kevin Gilkey-Graham * * @example * $('#example').dataTable( { @@ -21,7 +22,7 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, { "ip-address-pre": function ( a ) { var i, item; - var m, n, t; + var m, n, t, p; var x, xa; if (!a) { @@ -33,6 +34,7 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, { t = a.split(":"); if (t.length == 2){ m = t[0].split("."); + p = t[1]; } else { m = a.split("."); @@ -45,16 +47,10 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, { // IPV4 for(i = 0; i < m.length; i++) { item = m[i]; - - if(item.length == 1) { - x += "00" + item; - } - else if(item.length == 2) { - x += "0" + item; - } - else { - x += item; - } + x += "000".substr(item.length) + item; + } + if (p) { + x += ":" + "00000".substr(p.length) + p; } } else if (n.length > 0) { @@ -70,20 +66,8 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, { if(item.length === 0) { count += 0; } - else if(item.length == 1) { - xa += "000" + item; - count += 4; - } - else if(item.length == 2) { - xa += "00" + item; - count += 4; - } - else if(item.length == 3) { - xa += "0" + item; - count += 4; - } else { - xa += item; + xa += "0000".substr(item.length) + item; count += 4; } }