IP address sorting

IP address sorting

spacemancwspacemancw Posts: 31Questions: 9Answers: 0

I have a table with IP addresses in the first column. I am using the ip-address plugin and the automatic type detection plug-in for the ip-address type. This works. IP addresses are sorted correctly when the page loads.

I have other pages where there are IP address in columns other than the first column, and I load the same plugin. When I sort by IP column, the IPs are not in the correct order. Is this expected? Do the IPs have to be in the first column?

thanks

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi spacemancw,

    In this live example here, the IP address is in the second column. If you order by that, the search works as expected in that it searches alphabetically, which most likely isn't what you want.

    If you want to sort numerically, you need to modify the custom sorting. Take a look at this thread from before, which links to this plug-in code.

    Hope that helps,

    Cheers,

    Colin

  • spacemancwspacemancw Posts: 31Questions: 9Answers: 0

    Colin,
    thanks for the reply. I clicked on your example, but I do not see an IP address in any of the columns.
    As for the plug-in, I had mentioned that I am using the plug-in.

    thanks.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    The plug-in should work in any column. If the type detection is being included as well as the sorting plug-in, and it isn't picking up the IP addresses, that suggests that some piece of data in the column isn't matching the format.

    If you can give a link to the page showing the issue I can take a look and see what it is. Or use the debugger which should let me know what is needed.

    Allan

  • spacemancwspacemancw Posts: 31Questions: 9Answers: 0

    Allan, thanks for the reply. The site is an internal site for network monitoring. So I can't link to it.
    But I think I have a clue as to why this isn't working.
    The first table is a list of IP addresses, with every cell in the IP column populated with an IP address.
    However my other table is a list of devices in the first column and the the IP column gets populated IF there is an IP address, but blank if there is no IP address.

    So the column actually has a list of IPs and blanks. I suspect the blanks are throwing off the sorting.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    That would do it. Looking at the plug-in, it doesn't currently support an empty string. The type detection is always expecting an IPv4 address, while the sorting part actually allows IPv4 or IPv6. Neither supports an empty string. That would need a little modification to the plug-in.

    Allan

  • spacemancwspacemancw Posts: 31Questions: 9Answers: 0

    Allan,

    thanks for the reply again. As I don't know JS I can't mess with the plugin code.
    So I messed with my HTML.
    My bash shell script that builds the table looks for an IP.
    I changed the script to say if IP = "" then set IP to 0.0.0.0 and set the variable CTAG to "color: transparent;"

    the line for building that cell is:

    <td style=\"font-weight: bold; $CTAG\" align=left>$WANIP</td>

    So when there is no IP, this line becomes:

    <td style="font-weight: bold; color: transparent;" align=left>0.0.0.0</td>

    In the table the cells still appear blank as the text is transparent and the column sorts correctly.

    Thanks

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946
    edited March 2018 Answer ✓

    I was going to suggest something similar using columns.render. Also take a look at this page:
    https://datatables.net/manual/data/orthogonal-data

    You could do something like this in your IP column:

        render: function ( data, type, row ) {
            // If sort data is requested and data is empty 
            if ( type === 'sort' || data.length === 0 ) {
                return '0.0.0.0';
            }
     
            // Otherwise return original data for display and filter
            return data;
        }
    

    Kevin

  • spacemancwspacemancw Posts: 31Questions: 9Answers: 0

    Thanks Kevin,

    this looks pretty cool. I'm glad we were thinking along the same lines. I'll try it out.
    thanks again.

    S.

This discussion has been closed.