PDA

View Full Version : How to Sort grid with IP addresses



trcampion
4 Jul 2010, 1:18 AM
Can any one give an example of sorting a grid which has a column containing an IP address? I found one response to this question but am not sure how to implement it. I have a store pulling data successfully from a MYSQL table but cant figure out how the IP address field can be sorted correctly in my grid panel.

Animal
4 Jul 2010, 2:08 AM
http://www.sencha.com/deploy/dev/docs/?class=Ext.data.Field&member=sortType

trcampion
4 Jul 2010, 4:49 AM
Animal,

Thanks for the quick reply, and for all you posts to the forum I've learned quite a bit from your replies. The link you posted does show sorting but my problem is the field I'm trying to sort on contains an IP address and the sort methods referred to in the link don't give me the desired results (unless I'm missing something). My problem is described in the following post.

http://www.sencha.com/forum/showthread.php?92711-Sort-by-IP-address-in-Grid&highlight=sort+address+grid

I'm not sure how or where to apply the suggestion made by "micgala" in the reply to the post above. I was hoping to see a more verbose answer of what he was suggesting. Where would I insert the code he has suggested.

Animal
4 Jul 2010, 7:14 AM
How do you want to sort them?

trcampion
4 Jul 2010, 10:31 AM
Ascending, or descending (typically will want them ascending) but in correct numerical order. If you look at the link I posted earlier it describes the problem. The default behavior is to sort the field as a string which prevents the addresses from appearing in a correct order 192.168.1.10 would be placed in an order higher than 192.168.1.2 when sorted ascending. I suppose I could use a php function

http://php.net/ip2long
http://php.net/long2ip

and sort the data for the store before it gets loaded, but if I then sort by another field I can not get back to correct numerical IP order unless I reload the store.

Ideally if the fix from the earlier link posted reloves this behavior at a grid level that would be what I'm looking for. I just dont know how or where to implement what is suggested in that link. I did PM the "micgala" to see if he could elaborate on the details a bit more but have not seen a reply.

Thanks again for your assistance.

Tom C

Animal
4 Jul 2010, 11:11 AM
How? What IS correct order for IP addresses?

All you have to do is implement a sortTypes function which returns comparable values.

Animal
4 Jul 2010, 11:21 AM
SO mabye



var ipRe = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
function ip2Long(ip) {
var b = ip.match(ipRe);
console.dir(b);
return (parseInt(b[1], 10) << 24) |
(parseInt(b[2], 10) << 16) |
(parseInt(b[3], 10) << 8) |
parseInt(b[4], 10);
}

trcampion
4 Jul 2010, 12:20 PM
Animal,

I'll give the above a try and see if it can do what I want. I was also thinking there may be a simpler way for me to do this. If I create a new unsigned integer column in my database I can hold a value for the IP which should be easy to sort and achieve the correct result. This new integer field could be hidden in my grid. Can I use the values in this new hidden column as the basis of my sort for the visible column?