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
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: 3899
    
    9

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: 44048
    
  33
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: 20057
    
  44

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Sorting an Array List
 
jQuery in Action, 3rd edition