# Sort by IP in Sharepoint list

I have a column in Sharepoint containing IP-addresses. I can't figure out how to sort them correctly.

They are being sorted like this:

192.168.1.1

192.168.1.10

192.168.1.100

192.168.1.2

Rather than

192.168.1.1

192.168.1.2

192.168.1.10

192.168.1.100

However, I have tried the formula suggestions and they simply don't work correctly. I have tried by using two columns with a formula each and by using one column with the combined formula:

=LEFT([Internal IP];7)&RIGHT([Internal IP];3)

It seems silly to me that sorting numbers would be such a hassle. Have I missed something?

-
The sort is not considering numbers, but a piece of text (string). So, it's behaving as expected. –  Dante Jan 11 '11 at 12:49
The only thing I can think of is to insert a, b or c in front of each number based on if it's 0 to 9, 10 to 99 or 100 to 255 and then sort it that way. It still seems like a lot of hassle though. Besides if I theoretically wanted an ip like "10.10.10.x" it would fail my first sort rule because it has less numbers than 192.168.1.x. That Microsoft thread is hardly a solution even if the code had sorted the IP's correctly, because it doesn't accept adding an IP with a different length before the last octet. –  jman Jan 11 '11 at 13:57

This is actually pretty easy, I've done this in SQL Server for a long time. You just need to stop thinking of the data as an IP address. There is an old trick called bit twiddling that can be used to convert the value to an integer first. For example, 192.168.0.1 becomes 3232235521. Integers are fast and easy to sort. After doing the conversion, you drop the integer into a hidden computed column and do the sort on that column.

So if you have a text column called IPAddress and a calculated field called Integer, you would add this formula to compute the Integer column - it's really just a little binary math and you only end up storing an additional Int32 by treating each octet as a byte:

``````=VALUE(IF(NOT(ISBLANK([IPAddress]));(LEFT([IPAddress];
``````

Then create a view that sorts by the hidden calcualted field Integer. If you need to sort IPv6 addresses, the concept is the same, but you're converting from hex values to binary to integers and you'll use more storage.

-
Wow. Thanks a lot Joshua. This was really useful! Works like a charm. –  jman Jan 12 '11 at 8:43

If Joshua' formula doesn't quite work for you, you may need to substitute all the semicolons for commas.

``````=VALUE(IF(NOT(ISBLANK([IPAddress])),(LEFT([IPAddress],