Results 1 to 9 of 9
  1. #1
    nilesh_123 is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    No need to worry about that.

    Java 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]);
            }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    That sorts in character sequence. What about dotted IP address sequence? Will sorts of the two types of data give the same results?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    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

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    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.

Similar Threads

  1. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  2. Heap Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-16-2008, 10:27 PM
  3. Merge Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:43 PM
  4. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:42 PM
  5. Getting network card address in Java 6
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •