File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to sort ip addresses?

 
lokesh pattajoshi
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2576
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
lokesh pattajoshi
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2576
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf sir and Jan i am using below codes and it is working fne...
 
Shashidhar Kache
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shashidhar - did you have a question about the code you posted or was it just a suggested solution ?
 
Winston Gutkowski
Bartender
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic