Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:42:29+00:00 2026-06-09T10:42:29+00:00

I’m tring to sort an arraylist of String alphabetically, I tried everything, from the

  • 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:42:32+00:00Added an answer on June 9, 2026 at 10:42 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.