Results 1 to 7 of 7
Thread: Sorting List from specific value
- 12-14-2012, 05:19 AM #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.
That is Google's IP by the way.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
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
-
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, 06:17 AM #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?
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.Java Code:while (scan.hasNext()) { String ip = scan.nextLine(); strings.add(ip); if (ip.contains(value)) { ipTotal++; } System.out.println(ip);
- 12-14-2012, 09:26 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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:
And write the compareTo to order by millis.Java Code:public class <Some suitable name> implements Comparable { private String line; private int millis; ... etc etc }Please do not ask for code as refusal often offends.
- 12-15-2012, 09:08 AM #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
- 12-17-2012, 10:37 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 12-17-2012, 11:23 AM #7
Member
- Join Date
- Jul 2012
- Location
- Earth
- Posts
- 75
- Rep Power
- 0
Re: Sorting List from specific value
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
-
sorting linked list?
By cherrychives in forum New To JavaReplies: 3Last Post: 05-11-2012, 12:51 PM -
getting specific information from the array list
By elvis0288 in forum New To JavaReplies: 7Last Post: 03-05-2012, 12:40 PM -
Sorting a list
By sivasiv in forum New To JavaReplies: 3Last Post: 01-10-2012, 08:52 PM -
list sorting problem.
By bit_bit in forum Advanced JavaReplies: 2Last Post: 02-26-2010, 04:17 AM -
List Sorting method.
By bit_bit in forum New To JavaReplies: 1Last Post: 02-24-2010, 11:44 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks