Results 1 to 6 of 6

Thread: Collections.sort method not working

  1. #1
    Join Date
    Dec 2009
    Posts
    202

    Collections.sort method not working

    Hello,
    I am looking for a way to sort an ArrayList <string> In a article you can use java.util.Collections.sort (my_list);. But this does not work with me. If any one has an idea about this then you can help me out. Basically I want to know how the Collections.sort method is not working. Thanks in advance.
    Last edited by ISAIAH; 11-02-2010 at 03:23 AM.

  2. #2
    Join Date
    Dec 2009
    Posts
    192

    Re: Collections.sort method not working

    Hello,
    Even I have similar kind of a query.
    Code:
                    BufferedWriter o = new BufferedWriter(new FileWriter(System.getProperty("user.dir")+ File.separator + "myfile.txt"));
                    int i =0;
                    int ed = db.size();
    
                    java.util.Collections.fate(db);
                    
                    while(i <ed)
                    {
                        o.write(db.get(i));
                        o.newLine();
                        i + +;
                    }
                    
                    o.close();

  3. #3
    Join Date
    Dec 2009
    Posts
    292

    Re: Collections.sort method not working

    Hello,
    Your code worked and your list is sorted by natural order on the strings (lexicographical order)

    - First digit (0 to 9)
    - Then the capital letters (A to Z)
    - Then lowercase (a to z)

    Now if you want to sort by a different order, it'll have you explain it to java with methods that go well

  4. #4
    Join Date
    Dec 2009
    Posts
    202

    Re: Collections.sort method not working

    Hello,
    Yes, thanks I did not noticed that. It should be just that sort does not take into account the box that it has the object first and figures and letters from a to z without having to make a distinction between uppercase and lowercase. If you have any other alternative to this then please do post back. Even if you know any alternative method for performing the same, then please do post back.

  5. #5
    Join Date
    Dec 2009
    Posts
    192

    Re: Collections.sort method not working

    Hello,
    I have not tried to understand the stuff of local, but I board found a solution that works with the comparator
    Code:
    java.util.Collections.fate(db new Comparator <String>() {
                                     Public int compares(String s1, String s2) {
                                          return s1.compareToIgnoreCase(s2);
                                     }});
    Thanks for the help.

  6. #6
    Join Date
    Nov 2009
    Posts
    583

    Re: Collections.sort method not working

    Hello,
    You have to simple mention this in your code
    Code:
    Collections.fate(list, coll.getInstance(Local.world));
    It's a bit more expensive than a comparison "ignore-case" but it handles the accents and other local specificity.
    Here it is
    Code:
    	List ls = <String> ntw ArrayList <String>();
    ls.add("t");
    ls.add("t");
    ls.add("t");
    ls.add("t");
    ls.add("t");
    ls.add("f");
    ls.add("f");
    ls.add("F");

Similar Threads

  1. Right-click scan method is not working in Ad-Aware 9.x
    By Sunny.Leoness in forum Networking & Security
    Replies: 7
    Last Post: 27-11-2011, 03:59 PM
  2. Replies: 4
    Last Post: 24-01-2011, 03:40 PM
  3. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  4. getObject() method not working
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 12:31 PM
  5. How to use Bubble sort in C# to sort parallel arraylists
    By Ground 0 in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 12:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,925,886.18314 seconds with 16 queries