Sorting an Array of String Objects

Discussion in 'Java' started by TheVooDooChild, Mar 22, 2005.

  1. 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.
    TheVooDooChild, Mar 22, 2005
    #1
    1. Advertising

  2. TheVooDooChild

    Ola Bini Guest

    TheVooDooChild wrote:
    > 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
    Ola Bini, Mar 22, 2005
    #2
    1. Advertising

  3. TheVooDooChild

    Oscar kind Guest

    Ola Bini <> wrote:
    > TheVooDooChild wrote:
    >> 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());


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Mar 22, 2005
    #3
  4. Thank you for the reply, this seems to work fine. Why do you say this
    is a destructive operation?
    TheVooDooChild, Mar 23, 2005
    #4
  5. 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.


    --
    Whyowhydidn'tsunmakejavarequireanuppercaselettertostartclassnames....
    Thomas G. Marshall, Mar 24, 2005
    #5
  6. TheVooDooChild

    Oscar kind Guest

    Thomas G. Marshall <> 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.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Mar 24, 2005
    #6
  7. TheVooDooChild

    Lisa Guest

    "Oscar kind" <> wrote in message
    news:...
    > Thomas G. Marshall <>

    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?

    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.
    Lisa, Mar 24, 2005
    #7
  8. TheVooDooChild

    Eric Sosman Guest

    Lisa wrote:
    > "Oscar kind" <> wrote in message
    > news:...
    >
    >>Thomas G. Marshall <>

    >
    > 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."

    --
    Eric Sosman, Mar 24, 2005
    #8
  9. TheVooDooChild

    Lisa Guest

    "Eric Sosman" <> wrote in message
    news:d1vfik$ako$...
    >
    >
    > Lisa wrote:
    > > "Oscar kind" <> wrote in message
    > > news:...
    > >
    > >>Thomas G. Marshall

    <>
    > >
    > > 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?
    Lisa, Mar 24, 2005
    #9
  10. TheVooDooChild

    Eric Sosman Guest

    Lisa wrote:
    > [...]
    > 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.

    --
    Eric Sosman, Mar 24, 2005
    #10
  11. Lisa coughed up:
    > "Oscar kind" <> wrote in message
    > news:...
    >> Thomas G. Marshall
    >> <>

    > 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?


    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]...


    --
    Framsticks. 3D Artificial Life evolution. You can see the creatures
    that evolve and how they interact, hunt, swim, etc. (Unaffiliated with
    me). http://www.frams.alife.pl/
    Thomas G. Marshall, Mar 24, 2005
    #11
  12. "Lisa" <> writes:

    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Mar 25, 2005
    #12
  13. Lasse Reichstein Nielsen coughed up:
    > "Lisa" <> writes:
    >
    >> 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.



    --
    "His name was Robert Paulson. His name was Robert Paulson. His name was
    Robert Paulson..."
    Thomas G. Marshall, Mar 25, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade
    Replies:
    4
    Views:
    30,400
    rahul891
    Nov 4, 2012
  2. mutinymike

    sorting an array of objects

    mutinymike, Sep 11, 2007, in forum: Java
    Replies:
    0
    Views:
    342
    mutinymike
    Sep 11, 2007
  3. markspace
    Replies:
    1
    Views:
    386
    markspace
    Jun 25, 2009
  4. Roedy Green
    Replies:
    1
    Views:
    442
    Roedy Green
    Jun 25, 2009
  5. Replies:
    2
    Views:
    434
Loading...

Share This Page