• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does Java know the alphabet?

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33700
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olly,
Yes, Java knows the alphabet. Collections.sort(your list) does a sort using the default (alphabetical) order.
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33700
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should probably mention that java.util.Arrays.sort(your list) sorts alphabetically if your "list" is really a String[].
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh dear. Sorry. Typing error.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic