Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-17-2008, 01:33 PM
Member
 
Join Date: Oct 2008
Posts: 1
Rep Power: 0
nilesh_123 is on a distinguished road
Default sort IP address in java
hi,
i want to sort this
"192.168.1.12"
"192.168.1.11"
"192.168.1.13"

please give me sample code or some tips.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-17-2008, 03:43 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Look in the API doc for the sort method. The Index link at the top of the API doc page then select S. Then read the doc for those classes. When you find one that looks useful, use Search to find code samples.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-17-2008, 07:39 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
As a simple hint, those all things are strings. Search on the web about how to sort strings. As Norm says, please read Java doc before ask a code and try something to implement by yourself.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-17-2008, 08:12 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
What you show are Strings that have the dots in the same columns.
What if the sub parts of the address are different lengths?

Then you'll need to convert the 4 parts of the address to another format(say byte) and concatenate them to a long, sort the longs and then extract the 4 bytes back to decimal digits.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-17-2008, 08:20 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
No need to worry about that.

Code:
        String[] arr = new String[]{"192.168.2.12", "192.168.1.1", "192.168.1.14"};
        
        java.util.Arrays.sort(arr);
        
        for(int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-17-2008, 09:52 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
That sorts in character sequence. What about dotted IP address sequence? Will sorts of the two types of data give the same results?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-18-2008, 07:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
I'm not clear what you mean by IP address sequence Norm.

In the sense of int, they are just 32 bits ints. You might sort them unsigned. If they have in int, easily can compare them, as I don in the last post using Array.sort()

If you have them as InetAddress's, call GetAddress() and stuff the
byte[4] into an int. Then do the same thing as above.

Working with strings is the most easiest way I think.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-18-2008, 05:29 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Take the case of these two addresses. Which should be first:
4.22.22.22
22.22.22.22

I'd say they are in ascending order.
A String sort would change their order
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-18-2008, 11:08 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Yes that make sense. Such like your values cannot work fine with strings. So you can use one of other two ways I have explain in the above post. What you have to make sure to working on with strings is, each address segment should be in three digits. Actually same number of digits are ok.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 09:04 PM
Heap Sort in Java Java Tip Algorithms 0 04-16-2008 11:27 PM
Merge Sort in Java Java Tip Algorithms 0 04-15-2008 08:43 PM
Bubble Sort in Java Java Tip Algorithms 0 04-15-2008 08:42 PM
Getting network card address in Java 6 Java Tip Java Tips 0 03-02-2008 08:10 PM


All times are GMT +2. The time now is 11:21 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org