Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java.util.Arrays.sort doesn't work

695 views
Skip to first unread message

Hal Vaughan

unread,
Aug 17, 2004, 3:21:46 PM8/17/04
to
I'm trying to sort an array of strings. It won't compile, and I'm using
something similar to examples. Here's what I have:

import java.util.*;

//Bunch of other stuff, including declaring a class

String[] aTest = {"one", "two", "three"};
aTest = java.util.Arrays.sort(aTest);

Whenever I compile this, I get:

src/JavaCheck.java:122: incompatible types
found : void
required: java.lang.String[]
aTest = java.util.Arrays.sort(aTest);
^
1 error

I don't get this. I have a java.lang.String[] as a parameter, yet it says
the parm is void.

So what's going on, and what am I overlooking or don't know about using
this?

Thanks!

Hal

Ian Shef

unread,
Aug 17, 2004, 3:37:00 PM8/17/04
to
Hal Vaughan <h...@thresholddigital.com> wrote in news:d66dnQKHWsH-x7_cRVn-
q...@comcast.com:

> I'm trying to sort an array of strings. It won't compile, and I'm using
> something similar to examples. Here's what I have:
>
> import java.util.*;
>
> //Bunch of other stuff, including declaring a class
>
> String[] aTest = {"one", "two", "three"};
> aTest = java.util.Arrays.sort(aTest);
>
> Whenever I compile this, I get:
>
> src/JavaCheck.java:122: incompatible types
> found : void
> required: java.lang.String[]
> aTest = java.util.Arrays.sort(aTest);
> ^
> 1 error
>
> I don't get this. I have a java.lang.String[] as a parameter, yet it
says
> the parm is void.

<SNIP>

Actually, the compiler is trying to tell you that java.util.Arrays.sort()
returns void, but you are attempting to assign the result to aTest, which
requires java.lang.String[].

See the javadocs for the sort method of java.util.Arrays. The javadocs are
your friend!

Your line 122 should probably look like:
java.util.Arrays.sort(aTest);

Note that there is no assignment; this sort method receives an Array of
Object_s (that implement Comparable), and returns with the Array sorted in
ascending order.

(If you don't have the javadocs, download them from java.sun.com and
install them. Once you learn to use them, you will find them valuable.)

--
Ian Shef
These are my personal opinions and not those of my employer.

Remco de Boer

unread,
Aug 17, 2004, 3:38:19 PM8/17/04
to

Hal Vaughan wrote:

> src/JavaCheck.java:122: incompatible types
> found : void
> required: java.lang.String[]
> aTest = java.util.Arrays.sort(aTest);
> ^
> 1 error
>
> I don't get this. I have a java.lang.String[] as a parameter, yet it says
> the parm is void.

The 'sort' method returns void, so you can't assign its result to the
String array. Try

String[] aTest = {"one", "two", "three"};

java.util.Arrays.sort(aTest);

instead.

Regards,
Remco

Hal Vaughan

unread,
Aug 17, 2004, 4:11:27 PM8/17/04
to
Ian Shef wrote:

Actually, I have the JavaDocs.  I had looked up "Sort" in the index, and was
reading the index.  I just forgot to click on the link to see how it was
actually used.  My bad.

Thanks for pointing it out!

Hal

0 new messages