aspose file tools*
The moose likes Beginning Java and the fly likes How to sort ip addresses? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to sort ip addresses?" Watch "How to sort ip addresses?" New topic
Author

How to sort ip addresses?

lokesh pattajoshi
Ranch Hand

Joined: Jul 29, 2009
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
Marshal

Joined: Mar 22, 2005
Posts: 41829
    
  63
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.?


Ping & DNS - my free Android networking tools app
lokesh pattajoshi
Ranch Hand

Joined: Jul 29, 2009
Posts: 130
Dittmer Sir thanks for your valuable replay.I want to sort those ip addresses like...

1)Original IP Adresses

192.156.11.22
192.157.11.22
192.156.111.22

2)Remove dot from ip adresses

1921561122
1921571122
19215611122

3)Sort it

19215611122
1921571122
1921561122

4)Once again adding dot
192.156.111.22
192.157.11.22
192.156.11.22


Kindly Suggest.


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41829
    
  63
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

Joined: Dec 20, 2006
Posts: 2499
    
    8

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:

1)Original IP Adresses
192.156.11.122 (a)
192.156.111.21 (b)

2)Remove dot from ip adresses
19215611122 (a)
19215611121 (b)

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

4)Once again adding dot
? 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)


OCUP UML fundamental and ITIL foundation
youtube channel
lokesh pattajoshi
Ranch Hand

Joined: Jul 29, 2009
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
Marshal

Joined: Mar 22, 2005
Posts: 41829
    
  63
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

Joined: Dec 20, 2006
Posts: 2499
    
    8

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

Joined: Jul 29, 2009
Posts: 130
Hi Ulf sir and Jan i am using below codes and it is working fne...
Shashidhar Kache
Greenhorn

Joined: Feb 06, 2013
Posts: 2
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3534
    
  15
Shashidhar - did you have a question about the code you posted or was it just a suggested solution ?


Joanne
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3534
    
  15
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

Joined: Mar 17, 2011
Posts: 7779
    
  21

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

Joined: Feb 06, 2013
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to sort ip addresses?