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 Does Java know the alphabet? 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 "Does Java know the alphabet?" Watch "Does Java know the alphabet?" New topic
Author

Does Java know the alphabet?

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Hello everyone
I am in desperate need of help right now. I need to find a way to sort a list of strings alphabetically. I should be able to create a method that will recognise the alphabet but I will take me forever! And I don't have much time! Does anyone know a built-in method or a package that will help me? I've looked through the java.lang.String package but there doesn't seem to be anything helpful in there

I'm not asking anyone to make a code for me, just a package name, web link or something to help me make this method will do fine.

Thank you


Olly
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

Olly,
Yes, Java knows the alphabet. Collections.sort(your list) does a sort using the default (alphabetical) order.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Wow that's good news Could someone tell in what package it is in? (i.e. do I need to "import" anything?)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Class Collections is in the package java.util, so yes, you need to import it. Note that you can find all classes, interfaces etc. of the standard Java API in the API documentation. You can download the documentation from the Java SE download page for offline viewing.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Olivier Legat:
...I've looked through the java.lang.String package but there doesn't seem to be anything helpful in there ...

It's the compareTo method. This is what's used behind the scenes when calling Collections.sort on a List of Strings.

In the suggestion to use Collections.sort, I think it was assumed that when you said a "list" of Strings, you meant a collection that implements java.util.List (like an ArrayList, for example). But maybe you meant "list" less formally -- as a way of saying a "bunch" of Strings. In that case, you would need to put the Strings into a real List before using Collections.sort. On the other hand, maybe that's not really what you want. Let us know if this doesn't make sense.

(By the way, java.lang is a package, and java.lang.String is a class.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

I should probably mention that java.util.Arrays.sort(your list) sorts alphabetically if your "list" is really a String[].
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Thank-you Mr. Boyarsky Does this java.util.Arrays.sort(your list) also work for 2D arrays though?
[ February 25, 2008: Message edited by: Olivier Legat ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41046
    
  43
Originally posted by Olivier Legat:
Thank-you Mr. Boyarsky

Be careful what you call Jeanne - she might not like being called Mr. :-)

Does this java.util.Arrays.sort(your list) also work for 2D arrays though?

No. Lists in this context are one-dimensional only.


Ping & DNS - my free Android networking tools app
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Oh dear. Sorry. Typing error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does Java know the alphabet?
 
Similar Threads
eclipse configuration
Speech recognition
finding out difference in time?
Java array help
Help ...Dynamic Drop Down list