# Thread: Sorting List from specific value

1. Member
Join Date
Oct 2012
Posts
15
Rep Power
0

## 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. ## 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. Member
Join Date
Oct 2012
Posts
15
Rep Power
0

## 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();
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. Moderator
Join Date
Apr 2009
Posts
12,966
Rep Power
22

## 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.

5. Member
Join Date
Oct 2012
Posts
15
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
12,966
Rep Power
22

## Re: Sorting List from specific value

Read the line and then split() on something suitable, which looks like a space to me.

7. Member
Join Date
Jul 2012
Location
Earth
Posts
75
Rep Power
0

## Re: Sorting List from specific value

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.

#### Posting Permissions

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