sorting a LinkedList consisting of two items

L

learningjava

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
 
J

John C. Bollinger

learningjava said:
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
(e-mail address removed)
 
L

learningjava

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

learningjava said:
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
(e-mail address removed)
 
L

learningjava

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
(e-mail address removed)
 
J

John C. Bollinger

learningjava said:
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
(e-mail address removed)
 
J

John C. Bollinger

learningjava said:
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
(e-mail address removed)
 
L

learningjava

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
(e-mail address removed)
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top