Results 1 to 8 of 8

Thread: sort

  1. #1
    Camden is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

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

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    You can read the words from a file like so, and put them in the array:
    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");
    Then sort:
    Java Code:
    	java.util.Arrays.sort(arr);
    Last edited by staykovmarin; 11-27-2007 at 03:37 AM.

  3. #3
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Then convert it to a Vector.

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    		    Vector<String> v = new Vector<String>();
    		    for (int i = 0; i < arr.length; i++) {
    		    	v.add(arr[i]);
    		    }
    Java Forums &#187; General Java &#187; New To Java > spoonfeed

  5. #5
    Camden is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default

    you are very rude young man.
    any serious answer please?

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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:
    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);
    }
    what should I do? add them in the vector with .add() and then rearrange them and put them in a new vector? and how?
    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.
    Java Code:
    Collections.sort(v, your_Comparator);
    is this situation suitable for a stack use
    Maybe; depends on what you want to do.

  7. #7
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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.

  8. #8
    Camden is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default

    You insult yourself. And insulting is the easiest thing, especially when someone is hiding behind the screen.

Similar Threads

  1. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  2. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM
  3. how to sort
    By Feng in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 06:56 AM
  4. Heap Sort
    By kesav2005 in forum Advanced Java
    Replies: 1
    Last Post: 11-13-2007, 11:40 AM
  5. how to sort 2 tables
    By valery in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:30 PM

Posting Permissions

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