aspose 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
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: 3855

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: 39869
    
  28
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: 18997
    
  40

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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting an Array List