Sort Array Problem

J

Jenny

I tried the code below. It did not work. I even created an Object[]
to hold Integers 1,2 and 3, it still did not work. Could you help me
to sort 1,2,3 to be 3,2,1? I know how to write code to sort it. I'd
like to learn the Comparator. Thanks a lot.

Comparator myComp = new Comparator(){
public int compare(Object o1, Object o2){
int[] a1 = (int[])o1;
int[] a2 = (int[])o2;
if(a1[0] < a2[0]){
return 1;
}else if(a1[0] > a2[0]){
return -1;
}else{
return 0;
}
}
};
int ans[] = {1,2,3};
Arrays.sort(ans,myComp);
 
P

Paul Lutus

Jenny said:
I tried the code below. It did not work.

No, it didn't compile. Make it compile, then make a statement about how it
works or doesn't. Hint: Arrays.sort() cannot sort arrays of primitives.
 
S

Sudsy

Paul said:
No, it didn't compile. Make it compile, then make a statement about how it
works or doesn't. Hint: Arrays.sort() cannot sort arrays of primitives.

Um, better check your javadocs...
 
J

jungi

Hello Jenny,
I tried the code below. It did not work. I even created an Object[]
to hold Integers 1,2 and 3, it still did not work. Could you help me
to sort 1,2,3 to be 3,2,1? I know how to write code to sort it. I'd
like to learn the Comparator. Thanks a lot.

Comparator myComp = new Comparator(){
public int compare(Object o1, Object o2){
int[] a1 = (int[])o1;
int[] a2 = (int[])o2;
if(a1[0] < a2[0]){
return 1;
}else if(a1[0] > a2[0]){
return -1;
}else{
return 0;
}
}
};
int ans[] = {1,2,3};
Arrays.sort(ans,myComp);

if you want to learn comparator yourself, see:
tutorial:
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

API:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#reverseOrder()
http://java.sun.com/j2se/1.4.2/docs...ort(java.lang.Object[], java.util.Comparator)

--jungi
 
F

Fred L. Kleinschmidt

Jenny said:
I tried the code below. It did not work. I even created an Object[]
to hold Integers 1,2 and 3, it still did not work. Could you help me
to sort 1,2,3 to be 3,2,1? I know how to write code to sort it. I'd
like to learn the Comparator. Thanks a lot.

Comparator myComp = new Comparator(){
public int compare(Object o1, Object o2){
int[] a1 = (int[])o1;
int[] a2 = (int[])o2;
if(a1[0] < a2[0]){
return 1;
}else if(a1[0] > a2[0]){
return -1;
}else{
return 0;
}
}
};
int ans[] = {1,2,3};
Arrays.sort(ans,myComp);

No, this is not correct. The Arrays class has a sort method for sorting
arrays of objects. An array of ints is not an array of objects.

You have two choices. You can sort the integer array using
Arrays.sort(ans) and then reverse the order of the items in the array to
get them in descending order,

-- or --

you can create an Integer array, and sort it using Arrays.sort(Object[],
Comparator).
Note the capital "I" in Integer - this is a class, where integer is not.
 
A

ak

I tried the code below. It did not work.
No, it didn't compile. Make it compile, then make a statement about how it
works or doesn't. Hint: Arrays.sort() cannot sort arrays of primitives.

As I alredy said, read some java stuff...
 
P

Paul Lutus

Paul said:
No, it didn't compile. Make it compile, then make a statement about how it
works or doesn't. Hint: Arrays.sort() cannot sort arrays of primitives.

I posted too concisely for the trolls. I meant to say, apropos the OP's goal
of learning Comparator:

Arrays.sort(Object[],Comparator) cannot sort arrays of primitives.
 
A

ak

No, it didn't compile. Make it compile, then make a statement about how
it
I posted too concisely for the trolls. I meant to say, apropos the OP's goal
of learning Comparator:
who posts concisely wrong answers is the biggest troll.
 
P

Paul Lutus

ak said:
who posts concisely wrong answers is the biggest troll.

Among other traits, trolls can't bring themselves to post topically.
Consider this behavioral breakthrough.
 
A

ak

I posted too concisely for the trolls. I meant to say, apropos the OP's
Among other traits, trolls can't bring themselves to post topically.
Consider this behavioral breakthrough.
Consider to post answers only if you want to help
 
J

Jim Cochrane

Consider to post answers only if you want to help

Have you two entered a contest with each other to see who can end up
in the largest number of kill files? If so, I'd say you're about even,
so far.
 
A

ak

I posted too concisely for the trolls. I meant to say, apropos the
OP's
Have you two entered a contest with each other to see who can end up
in the largest number of kill files? If so, I'd say you're about even,
so far.

Sorry, that was dumb from me, but such things drives me crazy.
ok new moon - new begin. This war ends now and here.
 
B

Babu Kalakrishnan

Jenny said:
I tried the code below. It did not work. I even created an Object[]
to hold Integers 1,2 and 3, it still did not work.

Using an Object array (Or an Integer[]) and storing Integer objects should have
worked if your comparator was implemented correctly. (See below : I'll assume
that your array to be sorted is an Object[] or Integer[] that holds Integer
objects created with the "new Integer(value)" construct.)

Could you help me
to sort 1,2,3 to be 3,2,1? I know how to write code to sort it. I'd
like to learn the Comparator. Thanks a lot.

Comparator myComp = new Comparator(){
public int compare(Object o1, Object o2){
int[] a1 = (int[])o1;
int[] a2 = (int[])o2;
if(a1[0] < a2[0]){
return 1;
}else if(a1[0] > a2[0]){
return -1;
}else{
return 0;
}
}
};

The arguments with which the comparator's compare method is called are always
members of the array - (i.e Objects )- not the array itself (that's what you
seemed to assume). And all that the comparator needs to do is to return an
integer result that is negative,zero or positive indicating the result of the
comparison between those two arguments. So in your case the arguments you
receive will be Objects of type "Integer", and the method can be written as :

public int compare(Object o1, Object o2)
{
Integer a1 = (Integer)o1;
Integer a2 = (Integer)o2;
return a1.intValue()-a2.intValue(); // for reverse sort
}

Note the simplification in the last line. Since the comparator doesn't insist
that the result be any specific value (like 1 or -1), when the arguments are
unequal, 3 comparisons are unnecessary - a simple subtraction would do since it
will return a result with the proper sign.

BK
 
C

Carl Howells

Babu said:
public int compare(Object o1, Object o2)
{
Integer a1 = (Integer)o1;
Integer a2 = (Integer)o2;
return a1.intValue()-a2.intValue(); // for reverse sort
}

Note the simplification in the last line. Since the comparator doesn't
insist that the result be any specific value (like 1 or -1), when the
arguments are unequal, 3 comparisons are unnecessary - a simple
subtraction would do since it will return a result with the proper sign.

Well, you were so close to being correct. You understood her
fundamental problem much better than anyone else who posted did. But
that above advice is simply wrong.

What happens when that compare is called on these objects?

Integer i1 = new Integer(2147483647);
Integer i2 = new Integer(-2147483646);

Assuming the comparator instance is "comp", comp.compare(i1, i2) returns
-3. Last I checked, i1 is not the smaller value there.
 
B

Babu Kalakrishnan

Carl said:
Well, you were so close to being correct. You understood her
fundamental problem much better than anyone else who posted did. But
that above advice is simply wrong.

What happens when that compare is called on these objects?

Integer i1 = new Integer(2147483647);
Integer i2 = new Integer(-2147483646);

Assuming the comparator instance is "comp", comp.compare(i1, i2) returns
-3. Last I checked, i1 is not the smaller value there.

Good point. The simplified code above would fail on integer overflows.
I stand corrected. Thanks for pointing it out.

BK
 
T

Thomas G. Marshall

Jim Cochrane coughed up:
Have you two entered a contest with each other to see who can end up
in the largest number of kill files? If so, I'd say you're about
even, so far.

I had to go into groups.google.com to see whom you were talking about, since
one of them is a near-permanent denizen of my bozo-bin, and there were no
attributions in the reply bodies.

Based upon the content I guessed correctly, and I'd be really surprised if
the readers of this ng don't all agree upon who is /far/ more plonked.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top