• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to sort ip addresses?

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf sir and Jan i am using below codes and it is working fne...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shashidhar - did you have a question about the code you posted or was it just a suggested solution ?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Space pants. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic