Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about Collections.sort()  RSS feed

 
MuraliH Krishna
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
can any one kindly explain what is the mistake I am doing in the following code. Why it is not compiling.

import java.util.*;
public class CollectionTest1 {
public static void main(String[] a)
{
Set<animal> s = new HashSet<animal>();
s.add(new animal("aa"));
s.add(new animal("ab"));
s.add(new animal("ac"));
s.add(new animal("ad"));

animal[] ar = (animal[])s.toArray();
Collections.sort(s);

}

}

class animal implements Comparable<animal>
{
String i;
animal(String i)
{
this.i = i;
}
public String getI()
{
return i;
}
public int compareTo(animal a1) {
return i.compareTo(a1.getI());
}
}

Thanks in advance.
 
Henry Wong
author
Sheriff
Posts: 22840
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why it is not compiling.


It would help if you tell us the compiler error that you are getting.

Henry
 
Erica Barbato
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Collections.sort needs a List as parameter,but you are using a Set
[ January 20, 2008: Message edited by: erishine ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!