Sorting List from specific value

• 12-14-2012, 06:19 AM
sethe23
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.
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
• 12-14-2012, 06:22 AM
Fubarable
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.
• 12-14-2012, 07:17 AM
sethe23
Re: Sorting List from specific value
No, that method is new to me. Would I be able to target a specific value?
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.
• 12-14-2012, 10:26 AM
Tolls
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:
Code:

```public class <Some suitable name> implements Comparable {   private String line;   private int millis; ... etc etc }```
And write the compareTo to order by millis.
• 12-15-2012, 10:08 AM
sethe23
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
• 12-17-2012, 11:37 AM
Tolls
Re: Sorting List from specific value
Read the line and then split() on something suitable, which looks like a space to me.
• 12-17-2012, 12:23 PM
sabre150
Re: Sorting List from specific value
Quote:

Originally Posted by Tolls
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.