How to Sort a Collection or a List of tuples?

Hi everybody!!

I have a Collection or a List like:

List<MyClass> list=new ArrayList<MyClass>();

The class MyClass have two atributes: private String info

private int quantity

I want to Sort my class top bottom comparing quantity in order to print the list like:

30000 info

20000 info

...

you know what I mean. I've looked in the api for MyComparator() or Collections.Sort() by I didn't understand their way of work and I got a lot of ununderstanable errors.

Some commented peace of code will help me a lot.

Thanks to all!!!!!!!!!!!

[618 byte] By [freddyscriptsa] at [2007-11-27 2:41:59]
# 1
Why aren't you posting the error messages?You need to write an implementation of Comparator, and use that one when you sort. (Or make MyClass implement Comparable)Kaj
kajbja at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 2
Sorry man, I'm quite newbie on programming.how could I make that MyClass Implements Comparable and then sort my list?
freddyscriptsa at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 3
> Sorry man, I'm quite newbie on programming.> > how could I make that MyClass Implements Comparable> and then sort my list?What version of Java do you use?
kajbja at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 4
5.0
freddyscriptsa at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 5

> 5.0

public class MyClass implements Comparable<MyClass> {

public int compareTo(MyClass o) {

//return a negative integer, zero, or a positive integer as

//this object is less than, equal to, or greater than the specified object.

return 0;

}

}

Kaj

kajbja at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks! Collection<MyClass> foo=new ArrayList<MyClass>();Collections.sort(foo);give me an error:cannot find symbol ...method sort. Some syntax stuff I guess, can you help me?Thanks a lot man u rock
freddyscriptsa at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 7
Post the exact error message.
kajbja at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 8

The code:

import java.util.*;

class MyClass implements Comparable<MyClass> {

private String id;

private int tresure;

public MyClass(String s,int ie) {

this.id=s;

this.tresure=ie;

}

public int compareTo(MyClass o) {

return 0;

}

}

public class Hola {

public static void main(String[]args) {

Collection<MyClass> proba=new ArrayList<MyClass>();

for(int i=0;i<10;i++) {

proba.add(new MyClass("hola",i));

}

Collections.sort(proba);

}

}

The error:

Hola.java :19: cannot find symbol

symbol:method sort<java.util.collections><MyClass>>

location: class java.util.collections

Collections.sort<proba>;

1error

freddyscriptsa at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 9
Sort takes a list as argument. Change the declaration of proba from Collection to List.Kaj
kajbja at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 10

You may like to refer to the API documentation:

* http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html

List appears to be a sub-interface of Collection; Thus, Collections.sort(Collection) is not allowed, as mentioned by kajbj, it takes List:

* http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html

Hope this makes sense to you.

Cheers,

yc

ycliana at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...
# 11
Yes! It worked!!Thank you all very much!
freddyscriptsa at 2007-7-12 3:06:19 > top of Java-index,Java Essentials,Java Programming...