Sorting an ArrayList of Strings Alphabetically in Java?

Hello, I'm working with an ArrayList of Strings comprised of animal objects with String names which can be entered and added. I want them ordered alphabetically, but I'm not sure how.

I know to override the CompareTo method after implementing comparable and to use the Collections.sort method, but then what, and how?

My idea was to somehow implement an ArrayList of characters and use charAt to find the compare two Strings with each other, but I can't really figure out if this is the best way, or if there is a better way.

Surely, I cannot be the first person to alphabetize a list of Strings in an array, so how is this typically done? Thanks!


I should add, I know to use CompareTo and Collections already. What I don't know is which algorithm to implement in CompareTo that will actually compare two Strings alphabetically.

Update 2:

Also, I should say, I'm working with an ArrayList of Animal objects, each with a String name. Sorry for the confusion.

4 Answers

  • 8 years ago
    Favorite Answer

    Read the link, it is your exact same problem.

  • 8 years ago

    As I recall, String already has a compareTo() method that orders them alphabetically. So in your Animal class, all you need to do is this:

    public int compareTo(Animal other)


    return name.compareTo(;


    assuming that the String field in Animal is called 'name'.

  • 8 years ago

    compareTo() is defined for the String class, so you just need to use that method.

    Here's an example:

  • kinkel
    Lv 4
    4 years ago

    What you're needing relies upon on the way you examine a textual content record. in case you examine the record line by utilising line, then you definitely in basic terms keep including indexes into the arraylist. in case you examine the finished record straight away, then you definitely in basic terms upload the finished record string into one index of the arraylist.

Still have questions? Get your answers by asking now.