Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Does Java know the alphabet?

 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • 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
 
author & internet detective
Posts: 40359
819
Eclipse IDE VI Editor Java
  • 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
Mac Chrome Windows
  • 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?)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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
Posts: 40359
819
Eclipse IDE VI Editor Java
  • 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
Mac Chrome Windows
  • 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 ]
 
Rancher
Posts: 43016
76
  • 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
Mac Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh dear. Sorry. Typing error.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic