For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2004 > Sorting Strings









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sorting Strings
Paul Morrison

2004-10-28, 8:58 am

What is the best way to sort an array of strings, by that i mean, sort each
individual string alphabetically, and maybe then have two arrays, one with
the sorted word and the other with the original word, so that they can be
cross referenced. At the moment I am considering using mergesort, is this a
good idea, or if there something more practical? I am writing a class which
finds anagrams from a large set of words.

Cheers

Paul


Paul H. van Rossem

2004-10-28, 8:58 am

On 28-10-2004 14:15, Paul Morrison wrote:
> What is the best way to sort an array of strings, by that i mean, sort each
> individual string alphabetically, and maybe then have two arrays, one with
> the sorted word and the other with the original word, so that they can be
> cross referenced. At the moment I am considering using mergesort, is this a
> good idea, or if there something more practical? I am writing a class which
> finds anagrams from a large set of words.
>
> Cheers
>
> Paul


See the help on Arrays.sort(Object[] a). This might help you.

Paul.
VisionSet

2004-10-28, 8:58 am


"Paul Morrison" <pm42@kent.ac.uk> wrote in message
news:clqnt7$qbu$1@athena.ukc.ac.uk...
> What is the best way to sort an array of strings, by that i mean, sort

each
> individual string alphabetically, and maybe then have two arrays, one with
> the sorted word and the other with the original word, so that they can be
> cross referenced. At the moment I am considering using mergesort, is this

a
> good idea, or if there something more practical? I am writing a class

which
> finds anagrams from a large set of words.
>


If you mean sort array: {"bag", "ape", "can"}
to: {"abg", "aep", "acn"}

then iterate the array doing this on each element:

char[] chs = myString.toCharArray()

Arrays.sort(chs);

newArray[i] = new String(chs);

--
Mike W


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com