This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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: 3816

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: 37926
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: 18507

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 agree. Here's the link:
subject: Sorting an Array List
Similar Threads
Static fields are not populated
Sharing array with Javascript in JSP?
Extracting certain values from a string
How do you create an array of typesafe-collections?
Hibernate Search indexing...w/ Compass