0

I'm tring to sort an arraylist of String alphabetically, I tried everything, from the simple way :

List<String> theList = new ArrayList<String>();
theList.add("silex");theList.add("soliton");
theList.add("snake");theList.add("supracanon");
Collections.sort(theList);

To something more exotic :

List<String> theList = new ArrayList<String>();
theList.add("silex");theList.add("soliton");
theList.add("snake");theList.add("supracanon");
Collections.sort(
  theList, 
  new Comparator<String>() 
  {
    public int compare(String lhs, String rhs) 
    {
      return lhs.compareTo(rhs);
    }
  }
);

But nothing works, what am I doing wrong? thanks.

ps : I'm looking at the content of the resulting ArrayList like this :

for (String temp:listeProduitPredit){
            System.out.println(temp);
        } 

the content of the list does not change before and after the sorting process.

============================================================================= Allright, this is the actual code, I have an EJB doing database access, one of it's methods is returning me list of string.

tha list of strings is suposed to be ordered like in the dictionnary (alphabetically) however the 'Collections.sort(rList)' does nothing (input = output)

public List<String> rechercherListeDeProduitCommencantPar(Integer gammeId, Integer familleId, String debutProduit) {
    Criteria c = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(Produit.class, "p");
    c.createAlias("p.famille", "f").createAlias("f.gamme", "g");

    if (gammeId != null) {
        c.add(Restrictions.eq("g.id", gammeId));
    }
    if (familleId != null) {
        c.add(Restrictions.eq("f.id", familleId));
    }
    if (!debutProduit.equals("")) {
        c.add(Restrictions.like("p.designation", debutProduit+"%"));
    }

    //getting only the interesting intels (product's name)
    List<String> rList = new ArrayList<String>();
    List<Produit> pList = c.list();
    for (Produit p : pList){
        rList.add(p.getDesignation());
    }
    Collections.sort(rList);
    return rList;
}

this is running on a Jboss AS 5.1 server, I tested it by using a for before and after, the list is not being sorted alphabetically but it is indeed being modified a little:

18:44:07,961 INFO  [STDOUT] Before=========
18:44:07,961 INFO  [STDOUT] SUMO VIE
18:44:07,961 INFO  [STDOUT] soliton
18:44:07,961 INFO  [STDOUT] snake
18:44:07,961 INFO  [STDOUT] SupraCanon
18:44:07,961 INFO  [STDOUT] Segolene
18:44:07,961 INFO  [STDOUT] silex
18:44:07,962 INFO  [STDOUT] After=========
18:44:07,962 INFO  [STDOUT] SUMO VIE
18:44:07,962 INFO  [STDOUT] Segolene
18:44:07,962 INFO  [STDOUT] SupraCanon
18:44:07,962 INFO  [STDOUT] silex
18:44:07,962 INFO  [STDOUT] snake
18:44:07,962 INFO  [STDOUT] soliton
13
  • 8
    How exactly does it "not work"? How are you running that code and how are you examining the result? See sscce.org Commented Aug 9, 2012 at 16:21
  • 1
    I've tried your code, and it looks fine, as seen here: ideone.com/lrUDO. Commented Aug 9, 2012 at 16:24
  • 1
    @assylias: It's not sorted, note that OP is inserting two elements per line. Commented Aug 9, 2012 at 16:25
  • 2
    @Eildosa: most likely the error is in the code you are still now showing us. Again: sscce.org Commented Aug 9, 2012 at 16:30
  • 2
    Show us the output you got and the output you expected, or we can't help. Commented Aug 9, 2012 at 16:30

2 Answers 2

3

Your "after" array is sorted alphabetically:

18:44:07,962 INFO  [STDOUT] After=========
18:44:07,962 INFO  [STDOUT] SUMO VIE
18:44:07,962 INFO  [STDOUT] Segolene
18:44:07,962 INFO  [STDOUT] SupraCanon
18:44:07,962 INFO  [STDOUT] silex
18:44:07,962 INFO  [STDOUT] snake
18:44:07,962 INFO  [STDOUT] soliton

It's just that upper-case letters take precedence.

EDIT: If you want a case-insensitive sort, use:

theList.add("SUMO VIE");theList.add("soliton");
theList.add("snake");theList.add("supracanon");
Collections.sort(theList, String.CASE_INSENSITIVE_ORDER);

as suggested by Natix below.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh god how could I not see that xD?! thanks. yes I'd like to sort them case-insensitively. I used the exotic snipet and added toLowerCase() in the return but if you have a better suggestion I'll gladly take it.
String.compareTo() uses the unicode code for ordering. You should use a Collator instead.
2

Upper case characters come before lowercase characters.

"SUMO VIE".compareTo("Segolene") < 0

To sort your string list in case insensitive manner, you can use this comparator:

Collections.sort(rList, String.CASE_INSENSITIVE_ORDER);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.