File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting an Array List" Watch "Sorting an Array List" New topic
Author

Sorting an Array List

Sarita KShetty
Greenhorn

Joined: Nov 05, 2008
Posts: 4
ArrayList<String> st = new ArrayList<String>();
str.add("abc");
str.add("123");
str.add("xyz");
Collections.sort(st);
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
Bartender

Joined: Mar 24, 2008
Posts: 3775

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Campbell Ritchie
Sheriff

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

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
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Consider Paul's rocket mass heater.
 
subject: Sorting an Array List
 
Similar Threads
Static fields are not populated
Hibernate Search indexing...w/ Compass
Sharing array with Javascript in JSP?
Extracting certain values from a string
How do you create an array of typesafe-collections?