Results 1 to 8 of 8
Thread: sort
- 11-27-2007, 03:09 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 13
- Rep Power
- 0
sort
i take from a file elements with readLine( ) and i want to sort them ignoring cases (they are words) and put them in a vector (not array).
what should I do? add them in the vector with .add() and then rearrange them and put them in a new vector? and how?
is this situation suitable for a stack use?
can anyone help plz :cool:Last edited by Camden; 11-27-2007 at 04:33 AM.
- 11-27-2007, 03:31 AM #2
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
You can read the words from a file like so, and put them in the array:
Then sort:Java Code:BufferedReader reader = new BufferedReader(new FileReader("test.txt")); String s; // edit2: changed to StringBuilder. StringBuilder.append(String s) is way faster than String concatenation StringBuilder tmp = new StringBuilder(); while ((s = reader.readLine()) !=null) { tmp.append(s.toLowerCase() + "\n"); } String[] arr = tmp.toString().split("\n");
Java Code:java.util.Arrays.sort(arr);
Last edited by staykovmarin; 11-27-2007 at 03:37 AM.
- 11-27-2007, 04:06 AM #3
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Then convert it to a Vector.
- 11-27-2007, 04:18 AM #4
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Java Forums » General Java » New To Java > spoonfeedJava Code:Vector<String> v = new Vector<String>(); for (int i = 0; i < arr.length; i++) { v.add(arr[i]); }
- 11-27-2007, 10:03 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 13
- Rep Power
- 0
you are very rude young man.
any serious answer please?
- 11-27-2007, 11:08 PM #6
i take from a file elements with readLine( ) and i want to sort them ignoring cases (they are words) and put them in a vector (not array).
From staykovmarin's posts:
what should I do? add them in the vector with .add() and then rearrange them and put them in a new vector? and how?Java Code:BufferedReader reader = new BufferedReader(new FileReader("test.txt")); Vector<String> v = new Vector<String>(); String line; while ((line = reader.readLine()) != null) { v.add(line); }
Vector implements the List interface which implements the Collection interface. Therefore you can use the Collections class to sort your Vector with a comparator that sorts the words with/in lower case. See the Comparator api. And see the "Sorting" section on this page Lesson: Algorithms in the Collections trail.
is this situation suitable for a stack useJava Code:Collections.sort(v, your_Comparator);
Maybe; depends on what you want to do.
- 11-27-2007, 11:36 PM #7
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
I would give up the fight if i were you. Reading is not one of Camden's strong points. Not to mention that if he put my two posts together, he had the exact same result, a Vector with all the information that he needs.
Lucky for him, at least this time he didnt get his posts deleted for refusing to put two and two together AND for attempting to insult me.
- 11-28-2007, 01:11 AM #8
Member
- Join Date
- Nov 2007
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
need help with bubble sort
By lowpro in forum New To JavaReplies: 3Last Post: 12-17-2007, 05:27 PM -
how to sort
By Feng in forum New To JavaReplies: 1Last Post: 11-20-2007, 06:56 AM -
Heap Sort
By kesav2005 in forum Advanced JavaReplies: 1Last Post: 11-13-2007, 11:40 AM -
how to sort 2 tables
By valery in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 08:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks