Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Tolls

Thread: Sorting List from specific value

  1. #1
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Sorting List from specific value

    Greetings,

    I have managed to play around some and come a little close, but nothing major.

    I have a program that reads ping results directly from a .txt file.

    I need to be able to classify the results from least response time to greatest response time.
    Java Code:
    Reply from 74.125.130.105: bytes=32 time=24ms TTL=50
    Reply from 74.125.130.105: bytes=32 time=23ms TTL=50
    Reply from 74.125.130.105: bytes=32 time=23ms TTL=50
    Reply from 74.125.130.105: bytes=32 time=23ms TTL=50
    Reply from 74.125.130.105: bytes=32 time=24ms TTL=50
    Reply from 74.125.130.105: bytes=32 time=23ms TTL=50
    Reply from 74.125.130.105: bytes=32 time=24ms TTL=50
    Reply from 74.125.130.105: bytes=32 time=23ms TTL=50
    Reply from 74.125.130.105: bytes=32 time=24ms TTL=50
    Reply from 74.125.130.105: bytes=32 time=24ms TTL=50
    That is Google's IP by the way.

    Anyways, specifically, how would I be able to Collections.sort my list by the value of time. I came up with the idea to group by (*ms).
    So if I could get it to read directly targeting the time and then sort it would be great. Thanks for the help, and when I get it working I will post the code
    for whoever is interested.

    -Seth

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Sorting List from specific value

    Have you considered using a PriorityQueue<T> for this purpose? If you create a Comparable-implementing class for your data and add it to this queue, it will sort it for you.

  3. #3
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Sorting List from specific value

    No, that method is new to me. Would I be able to target a specific value?
    Java Code:
    while (scan.hasNext())
            { 
                String ip = scan.nextLine();
                strings.add(ip);
        if (ip.contains(value))
                {
                   ipTotal++;
                }
              
                System.out.println(ip);
    This is just the last thing I really had in the code. Value is input from the user and not very important to the function I am trying to accomplish. But I want it to organized by the time. Value is just being searched from the whole string entirely, I want this function to target time. Thanks for the suggestion.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Sorting List from specific value

    I think Fubarable is suggesting turning each line into an object of a suitable class.
    It can even simply be a class consisting of:
    Java Code:
    public class <Some suitable name> implements Comparable {
       private String line;
       private int millis;
    ... etc etc
    }
    And write the compareTo to order by millis.
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    sethe23 is offline Member
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Sorting List from specific value

    Ok, I think I am following along. How would I get "millis" to be recognized from the list? Thanks, I seem to be getting somewhere now. :P

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Sorting List from specific value

    Read the line and then split() on something suitable, which looks like a space to me.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Sorting List from specific value

    Quote Originally Posted by Tolls View Post
    Read the line and then split() on something suitable, which looks like a space to me.
    One still then has to obtain the content of the field in the correct form for sorting. Since the OP needs to sort on a numeric value the field needs to be converted to a number (for an ascending sort/queue we do want 43ms to be ordered before 143ms).

    I would not use split() since it will not extract the part I need without further String processing . I would use Pattern and Matcher.find() looking for "=(\d+)ms" then one only has to process group 1 . Since this sounds like either a homework or simple personal project I would expect the OP to use String.indexOf() and then extract the decimal digits.

Similar Threads

  1. sorting linked list?
    By cherrychives in forum New To Java
    Replies: 3
    Last Post: 05-11-2012, 12:51 PM
  2. getting specific information from the array list
    By elvis0288 in forum New To Java
    Replies: 7
    Last Post: 03-05-2012, 12:40 PM
  3. Sorting a list
    By sivasiv in forum New To Java
    Replies: 3
    Last Post: 01-10-2012, 08:52 PM
  4. list sorting problem.
    By bit_bit in forum Advanced Java
    Replies: 2
    Last Post: 02-26-2010, 04:17 AM
  5. List Sorting method.
    By bit_bit in forum New To Java
    Replies: 1
    Last Post: 02-24-2010, 11:44 AM

Posting Permissions

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