Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# How to sort ip addresses?

lokesh pattajoshi
Ranch Hand
Posts: 130
Hi everyone,

I am getting string as ip addresses in following format i want to sort those.kindly help..

192.156.11.22
192.157.11.22
192.156.111.22
200.156.11.222
192.21.11.22

Thanks and Regards
Lokesh Kumar Pattajoshi

Ulf Dittmer
Rancher
Posts: 42968
73
Assuming that those are stored in a List collection, you can use the Collections.sort(List, Comparator) method with a custom Comparator class.

How should the list be sorted - numerically ascending by first triplet, then 2nd triplet, etc.?

lokesh pattajoshi
Ranch Hand
Posts: 130
Dittmer Sir thanks for your valuable replay.I want to sort those ip addresses like...

192.156.11.22
192.157.11.22
192.156.111.22

1921561122
1921571122
19215611122

3)Sort it

19215611122
1921571122
1921561122

192.156.111.22
192.157.11.22
192.156.11.22

Kindly Suggest.

Ulf Dittmer
Rancher
Posts: 42968
73
While I'm not sure of the algorithm -Is it numerically largest first? If so, are you sure that makes sense for all possible addresses?-, using a Comparator with a List can accomplish that. If you haven't used Comparators before, read http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

Jan Cumps
Bartender
Posts: 2602
13
Digging a bit deeper in the proposed algorithm:

If this is the algorithm you want to use, it will need some corrections. You will have to make all address parts 3 digits, with leading zeros.

This is what can happen by using the approach you explain:

192.156.11.122 (a)
192.156.111.21 (b)

19215611122 (a)
19215611121 (b)

3)Sort it
19215611121 (b)
19215611122 (a)
- This might not be the sorting order you want

? where should we put the dots?

If you do the intermediate step to make all segments 3 digits wide, you do not get the undesired sorting:

1.5)Convert to all segments 3 digits wide, leading zero
192.156.011.122 (a)
192.156.111.021 (b)

lokesh pattajoshi
Ranch Hand
Posts: 130
Dittmer sir thanks for your suggestions.

Hi Cumps

thanks a lot for your replay..will it give correct result if i am sorting it as string after making all the value as 3 digits value.kindly replay

Regards,
Lokesh Kumar Pattajoshi

Ulf Dittmer
Rancher
Posts: 42968
73
will it give correct result if i am sorting it as string after making all the value as 3 digits value.

We can't say, because you haven't told us what the sorting algorithm is.

Jan Cumps
Bartender
Posts: 2602
13
lokesh pattajoshi wrote:Hi Cumps

thanks a lot for your replay..will it give correct result if i am sorting it as string after making all the value as 3 digits value.kindly replay

Create a test using Ulf's Comparator proposal, and put our sorting algorithm in the compareTo(). Tell us if it worked.

lokesh pattajoshi
Ranch Hand
Posts: 130
Hi Ulf sir and Jan i am using below codes and it is working fne...

Shashidhar Kache
Greenhorn
Posts: 2

Joanne Neal
Rancher
Posts: 3742
16
Shashidhar - did you have a question about the code you posted or was it just a suggested solution ?

Winston Gutkowski
Bartender
Posts: 10527
64
Shashidhar,

1. Welcome to JavaRanch.
2. Please DontWriteLongLines (←click). I've broken yours up as best I can, but before you post again, please read the UseCodeTags page thoroughly.
3. In general, we don't like people to simply post solutions (I assume that was your intent). Far better to help OP, but let him/her work it out for themselves.

Winston

Joanne Neal
Rancher
Posts: 3742
16
Winston Gutkowski wrote:3. In general, we don't like people to simply post solutions (I assume that was your intent). Far better to help OP, but let him/her work it out for themselves.

The OP had posted their own solution and it was almost three years ago, so it's probably not a problem here.

Winston Gutkowski
Bartender
Posts: 10527
64
Joanne Neal wrote:The OP had posted their own solution and it was almost three years ago, so it's probably not a problem here.

Yeah, that's why I left it for posterity.

Winston

Shashidhar Kache
Greenhorn
Posts: 2
Joanne Neal wrote:Shashidhar - did you have a question about the code you posted or was it just a suggested solution ?

Shashidhar Kache wrote: It was just a suggested solution.