This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Sorting an Array List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting an Array List" Watch "Sorting an Array List" New topic

Sorting an Array List

Sarita KShetty

Joined: Nov 05, 2008
Posts: 4
ArrayList<String> st = new ArrayList<String>();
for(String x:str)
System.out.print(x + "");

What would be the output and could someone explain how is the contents of this array sorted?
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3899

What would be the output

Why not try your self?

Collections.sort() sorts the specified List in to ascending order, based on the natural ordering of its elements (in this case String). Refer the API

[How to ask questions] [Twitter]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44048
Welcome to JavaRanch.
When Vijitha Kumara referred you to the API, that probably meant this bit, this bit and this bit.
Sarita KShetty

Joined: Nov 05, 2008
Posts: 4
The output is 123abcxyz i want to know as to why it is 123abcxyz ...i mean why is that the numbers come first
Henry Wong

Joined: Sep 28, 2004
Posts: 20057

i mean why is that the numbers come first

The ordering is based on unicode (and for the letters and numbers unicode is the same as ASCII). The numbers come first because the numbers come first on the ASCII table.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Sorting an Array List
jQuery in Action, 3rd edition