
10-17-2008, 01:33 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 1
Rep Power: 0
|
|
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.
|
|

10-17-2008, 03:43 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
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.
|
|

10-17-2008, 07:39 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
|
|
|
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.
|
|

10-17-2008, 08:12 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
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.
|
|

10-17-2008, 08:20 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
|
|
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]);
} |
|
|

10-17-2008, 09:52 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
That sorts in character sequence. What about dotted IP address sequence? Will sorts of the two types of data give the same results?
|
|

10-18-2008, 07:43 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
|
|
|
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.
|
|

10-18-2008, 05:29 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
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
|
|

10-18-2008, 11:08 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
|
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
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