sorting a LinkedList consisting of two items

Discussion in 'Java' started by learningjava, Dec 10, 2003.

  1. learningjava

    learningjava Guest

    I have a LinkedList consisting of two items.
    Each item is an array or two elements and the linkes list looks like this:

    LinkedList nums = new LinkedList();
    int[] numset = new int[2] ;
    numset[0] = 1;
    numset[1] = 2;
    nums.add(numset);
    .....
    .....

    .....



    I want to be able to sort the linked list by the second integer of each element.

    Can I use Collections.sort(...) to achieve this?
    How?

    Thanks,

    gklinux
     
    learningjava, Dec 10, 2003
    #1
    1. Advertising

  2. learningjava wrote:

    > I have a LinkedList consisting of two items.


    The number of items is irrelevant. (Though you won't be able to check
    the result without at least two elements.)

    > Each item is an array or two elements and the linkes list looks like this:
    >
    > LinkedList nums = new LinkedList();
    > int[] numset = new int[2] ;
    > numset[0] = 1;
    > numset[1] = 2;
    > nums.add(numset);


    Do be sure to create a _new_ int[] for the second element, or you won't
    get the behavior you [probably] expect.

    > I want to be able to sort the linked list by the second integer of each element.
    >
    > Can I use Collections.sort(...) to achieve this?


    Yes.

    > How?


    Use the version that accepts a Comparator. Provide an instance of a
    Comparator that compares objects of the type you have (int[]) according
    to the criteria you want (value of the second array elements).


    John Bollinger
     
    John C. Bollinger, Dec 10, 2003
    #2
    1. Advertising

  3. learningjava

    learningjava Guest

    Thanks, John.
    Can you point me to a link which has this construct?


    > learningjava wrote:
    >
    > > I have a LinkedList consisting of two items.

    >
    > The number of items is irrelevant. (Though you won't be able to check
    > the result without at least two elements.)
    >
    > > Each item is an array or two elements and the linkes list looks like this:
    > >
    > > LinkedList nums = new LinkedList();
    > > int[] numset = new int[2] ;
    > > numset[0] = 1;
    > > numset[1] = 2;
    > > nums.add(numset);

    >
    > Do be sure to create a _new_ int[] for the second element, or you won't
    > get the behavior you [probably] expect.
    >
    > > I want to be able to sort the linked list by the second integer of each element.
    > >
    > > Can I use Collections.sort(...) to achieve this?

    >
    > Yes.
    >
    > > How?

    >
    > Use the version that accepts a Comparator. Provide an instance of a
    > Comparator that compares objects of the type you have (int[]) according
    > to the criteria you want (value of the second array elements).
    >
    >
    > John Bollinger
    >
     
    learningjava, Dec 11, 2003
    #3
  4. learningjava

    learningjava Guest

    John,

    I didn't understand what you meant by ceating a new int[] for the second element.
    If the two items are separate, how are we going to add them to the
    linked list?
    I don't know if we can do this :
    nums.add(num1, num2);
    Can we?




    > > I have a LinkedList consisting of two items.

    >
    > The number of items is irrelevant. (Though you won't be able to check
    > the result without at least two elements.)
    >
    > > Each item is an array or two elements and the linkes list looks like this:
    > >
    > > LinkedList nums = new LinkedList();
    > > int[] numset = new int[2] ;
    > > numset[0] = 1;
    > > numset[1] = 2;
    > > nums.add(numset);

    >
    > Do be sure to create a _new_ int[] for the second element, or you won't
    > get the behavior you [probably] expect.
    >
    > > I want to be able to sort the linked list by the second integer of each element.
    > >
    > > Can I use Collections.sort(...) to achieve this?

    >
    > Yes.
    >
    > > How?

    >
    > Use the version that accepts a Comparator. Provide an instance of a
    > Comparator that compares objects of the type you have (int[]) according
    > to the criteria you want (value of the second array elements).
    >
    >
    > John Bollinger
    >
     
    learningjava, Dec 11, 2003
    #4
  5. learningjava wrote:

    > John,
    >
    > I didn't understand what you meant by ceating a new int[] for the second element.
    > If the two items are separate, how are we going to add them to the
    > linked list?
    > I don't know if we can do this :
    > nums.add(num1, num2);
    > Can we?


    No you can't, at least not in Java 1.4 or below.

    You didn't show how you create and add the second element, so I was
    attempting to head off a mistake that is relatively common among
    inexperienced Java programmers. It goes something like this:

    /*
    * Do NOT do this unless you really mean it!
    */
    LinkedList nums = new LinkedList();
    int[] numset = new int[2];

    numset[0] = 1;
    numset[1] = 2;
    nums.add(numset);

    // The mistake begins here
    numset[0] = 3;
    numset[1] = 4;
    nums.add(numset);

    // Instead you should do this:
    // numset = new int[2];
    // numset[0] = 3;
    // numset[1] = 4;
    // nums.add(numset);


    As written above, your list would end up with two references to the
    *same* array, with necessarilly the *same* elements ( 3 and 4 when the
    code completes). Adding an object to a List does not create a copy of
    that object, but rather stores a new reference to it. To have two
    different arrays with independant elements you would use code such as
    that commented out above.


    John Bollinger
     
    John C. Bollinger, Dec 11, 2003
    #5
  6. learningjava wrote:

    > Thanks, John.
    > Can you point me to a link which has this construct?


    The API docs for java.util.Comparator are online at

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html

    You must write a class that implements that interface, and provide an
    instance of that class to Collections.sort(List, Comparator) along with
    your List. The compare(Object, Object) method of the Comparator
    implementation class will be invoked on each pair of objects that the
    sort wishes to compare, to determine which of the two should come before
    the other. The API docs describe how the method is expected to behave.

    >>Use the version that accepts a Comparator. Provide an instance of a
    >>Comparator that compares objects of the type you have (int[]) according
    >>to the criteria you want (value of the second array elements).



    John Bollinger
     
    John C. Bollinger, Dec 11, 2003
    #6
  7. learningjava

    learningjava Guest

    Thanks, John.
    I learnt something more today.

    gklinux
    > > I didn't understand what you meant by ceating a new int[] for the second element.
    > > If the two items are separate, how are we going to add them to the
    > > linked list?
    > > I don't know if we can do this :
    > > nums.add(num1, num2);
    > > Can we?

    >
    > No you can't, at least not in Java 1.4 or below.
    >
    > You didn't show how you create and add the second element, so I was
    > attempting to head off a mistake that is relatively common among
    > inexperienced Java programmers. It goes something like this:
    >
    > /*
    > * Do NOT do this unless you really mean it!
    > */
    > LinkedList nums = new LinkedList();
    > int[] numset = new int[2];
    >
    > numset[0] = 1;
    > numset[1] = 2;
    > nums.add(numset);
    >
    > // The mistake begins here
    > numset[0] = 3;
    > numset[1] = 4;
    > nums.add(numset);
    >
    > // Instead you should do this:
    > // numset = new int[2];
    > // numset[0] = 3;
    > // numset[1] = 4;
    > // nums.add(numset);
    >
    >
    > As written above, your list would end up with two references to the
    > *same* array, with necessarilly the *same* elements ( 3 and 4 when the
    > code completes). Adding an object to a List does not create a copy of
    > that object, but rather stores a new reference to it. To have two
    > different arrays with independant elements you would use code such as
    > that commented out above.
    >
    >
    > John Bollinger
    >
     
    learningjava, Dec 15, 2003
    #7
    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. Frodo Larik
    Replies:
    1
    Views:
    1,633
    J├╝rgen Exner
    May 30, 2004
  2. tolisss
    Replies:
    1
    Views:
    370
    =?Utf-8?B?dGlta2xpbmc=?=
    Nov 12, 2005
  3. Andrew Smith

    String consisting of spaces

    Andrew Smith, Oct 17, 2003, in forum: Java
    Replies:
    25
    Views:
    749
    Raymond DeCampo
    Oct 19, 2003
  4. Replies:
    2
    Views:
    704
    Beat Bolli
    Jun 21, 2005
  5. Maziar Aflatoun

    DataTable consisting of 2 other DataTables

    Maziar Aflatoun, Feb 5, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    131
    Elton Wang
    Feb 6, 2005
Loading...

Share This Page