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