Sorting an Array of String Objects

T

TheVooDooChild

I have a small array of String objects that I need to sort
alphabetically. Can someone please show me the best way to do this?

Thanks in advance.
 
O

Ola Bini

TheVooDooChild said:
I have a small array of String objects that I need to sort
alphabetically. Can someone please show me the best way to do this?

Thanks in advance.

String[] myArray = new String[] {"foo","bar","baz"};
java.util.Arrays.sort(myArray); //WARNING, destructive operation

/O
 
O

Oscar kind

Ola Bini said:
TheVooDooChild said:
I have a small array of String objects that I need to sort
alphabetically. Can someone please show me the best way to do this?
String[] myArray = new String[] {"foo","bar","baz"};
java.util.Arrays.sort(myArray); //WARNING, destructive operation
Or, if you want the sort to be case-insensitive, you can additionally use
a Collator. This is slightly more work, as shown below:

String[] myArray = new String[] {"foo","Bar","baz"};
java.util.Arrays.sort(myArray, java.text.Collator.getInstance());
 
T

TheVooDooChild

Thank you for the reply, this seems to work fine. Why do you say this
is a destructive operation?
 
T

Thomas G. Marshall

TheVooDooChild coughed up:
Thank you for the reply, this seems to work fine. Why do you say this
is a destructive operation?


He probably is simply flagging the fact that the array is being altered in
place irrecoverably.
 
O

Oscar kind

Thomas G. Marshall said:
TheVooDooChild coughed up:


He probably is simply flagging the fact that the array is being altered in
place irrecoverably.

Yes, because changing a method parameter is a side effect.

Normally, a method doesn't change it's parameters (side effects are
generally more difficult to test). So because
java.util.Arrays#sort(Object[]) does change it's parameter, he flags it.
 
L

Lisa

Oscar kind said:
TheVooDooChild coughed up:


He probably is simply flagging the fact that the array is being altered in
place irrecoverably.

Yes, because changing a method parameter is a side effect.

Normally, a method doesn't change it's parameters (side effects are
generally more difficult to test). So because
java.util.Arrays#sort(Object[]) does change it's parameter, he flags it.

Doesn't the sort method just change the reference "myArray" to refer to
a new object created by the sort method?

If so, if there are other references to the original array other
than "myArray" they are not changed since sort doesn't know about them.
 
E

Eric Sosman

Lisa said:
Thomas G. Marshall <[email protected]>
wrote:
TheVooDooChild coughed up:

Thank you for the reply, this seems to work fine. Why do you say this
is a destructive operation?


He probably is simply flagging the fact that the array is being altered
in
place irrecoverably.

Yes, because changing a method parameter is a side effect.

Normally, a method doesn't change it's parameters (side effects are
generally more difficult to test). So because
java.util.Arrays#sort(Object[]) does change it's parameter, he flags it.

Doesn't the sort method just change the reference "myArray" to refer to
a new object created by the sort method?

No, and there are two easy ways to see that this is
so. First, observe that the method is `void', meaning
that it returns no value. If it created a new array
behind the scenes somewhere, it would have no way to
return that new array to the caller.

The second way to discover this is even easier and
more straightforward, and requires no heavy thinking:
"When all else fails, read the Javadoc."
 
L

Lisa

Eric Sosman said:
wrote:

TheVooDooChild coughed up:

Thank you for the reply, this seems to work fine. Why do you say this
is a destructive operation?


He probably is simply flagging the fact that the array is being altered
in

place irrecoverably.

Yes, because changing a method parameter is a side effect.

Normally, a method doesn't change it's parameters (side effects are
generally more difficult to test). So because
java.util.Arrays#sort(Object[]) does change it's parameter, he flags it.

Doesn't the sort method just change the reference "myArray" to refer to
a new object created by the sort method?

No, and there are two easy ways to see that this is
so. First, observe that the method is `void', meaning
that it returns no value. If it created a new array
behind the scenes somewhere, it would have no way to
return that new array to the caller.

The second way to discover this is even easier and
more straightforward, and requires no heavy thinking:
"When all else fails, read the Javadoc."

Above it says: java.util.Arrays#sort(Object[])
and it appears to me that the parameter of sort is an object.
The javadoc says that String is immutable. How can a java
method change an immutable object?
 
E

Eric Sosman

Lisa said:
[...]
Above it says: java.util.Arrays#sort(Object[])
and it appears to me that the parameter of sort is an object.
The javadoc says that String is immutable. How can a java
method change an immutable object?

A String object is immutable. An array object is
mutable. sort() rearranges the elements of the array,
but does not change the objects those elements refer to.

Before:
array[0] -> "foo"
array[1] -> "bar"
array[2] -> "baz"

After:
array[0] -> "bar"
array[1] -> "baz"
array[2] -> "foo"

The String objects are unchanged; the array elements
that refer to them have been shuffled.
 
T

Thomas G. Marshall

Lisa coughed up:
Oscar kind said:
Thomas G. Marshall
TheVooDooChild coughed up:
Thank you for the reply, this seems to work fine. Why do you say
this is a destructive operation?


He probably is simply flagging the fact that the array is being
altered in place irrecoverably.

Yes, because changing a method parameter is a side effect.

Normally, a method doesn't change it's parameters (side effects are
generally more difficult to test). So because
java.util.Arrays#sort(Object[]) does change it's parameter, he flags
it.

Doesn't the sort method just change the reference "myArray" to refer
to a new object created by the sort method?

There is no way for that to even be possible.

Java is pass by value /only/. This means that the following:

String[] myArray = {"aaa", "ccc", "bbb"};
java.util.Arrays.sort(myArray);

cannot do a /thing/ to the reference itself, it is passed in as a value. If
it created a new arary object within the sort() method, it would have
nowhere permenant to put it. That is, if the sort method even was defined
as the following:

public void sort(Object[] objects)
{
object = null;
}

then the following calling:

String[] myArray = {"aaa", "ccc", "bbb"};
java.util.Arrays.sort(myArray);

would just result in nothing being done to the array at all.

....[rip]...
 
L

Lasse Reichstein Nielsen

Lisa said:
Above it says: java.util.Arrays#sort(Object[])
and it appears to me that the parameter of sort is an object.

It is an object. More precisely, it is an array of Object.
All arrays are objects.
The javadoc says that String is immutable. How can a java
method change an immutable object?

If it could, the object wouldn't be immutable :)

But notice that the actual argument is an array of String.
Each string is immutable, but the array itself is not.
It is modified by the sort method, so that its elements
is in a (potentially) different order.

/L
 
T

Thomas G. Marshall

Lasse Reichstein Nielsen coughed up:
Lisa said:
Above it says: java.util.Arrays#sort(Object[])
and it appears to me that the parameter of sort is an object.

It is an object. More precisely, it is an array of Object.
All arrays are objects.
The javadoc says that String is immutable. How can a java
method change an immutable object?

If it could, the object wouldn't be immutable :)

But notice that the actual argument is an array of String.
Each string is immutable, but the array itself is not.
It is modified by the sort method, so that its elements
is in a (potentially) different order.


Since this is a beginner question, it's probably important to point out that
array /lengths/ cannot change. So you can from time to time hear people say
things like "array lengths are immutable, but array contents are not.",
which is not a great way to put it but is true enough.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top