0

I have this class:

public class Media {
double supporto;
double i_m;
String idSentence;

public Media(double supporto,double i_m,String idSent){
    this.supporto=supporto;
    this.i_m=i_m;
    this.idSentence=idSent;
}


public double getSupporto(){
    return this.supporto;
}

public double getI_m(){
    return this.i_m;
}

public String getIdSentence(){
    return this.idSentence;
}

public String toString(){
    return this.supporto+" - "+this.i_m+" - "+this.idSentence;
}
}

and add in an ArrayList text these values​​:

ArrayList<Media> m=new ArrayList<Media>();
m.add(new Media(0.2545,0.2365,"id002"));
m.add(new Media(0.8745,0.4658,"id005"));
m.add(new Media(0.1599,0.6580,"id0010"));

How do I sort this array in descending order of the value of the first class Media? I want to get this order:

 0.1599,0.6580,"id0010"
 0.2545,0.2365,"id002"
 0.8745,0.4658,"id005"

EDIT

I solved this way:

public class Media implements Comparable<Object>{
double supporto;
double i_m;
String idSentence;

public Media(double supporto,double i_m,String idSent){
this.supporto=supporto;
this.i_m=i_m;
this.idSentence=idSent;
}
public double getSupporto(){
return this.supporto;
}
public double getI_m(){
return this.i_m;
}
public String getIdSentence(){
return this.idSentence;
}
public String toString(){
return this.supporto+" - "+this.i_m+" - "+this.idSentence;
}
@Override
public int compareTo(Object arg0) {
    if(this.supporto==((Media) arg0).getSupporto()) return 0;
    else if((this.supporto)>((Media)arg0).getSupporto()) return 1;
    else return -1;
}
}

In the main method I order the m ArrayList:

    Collections.sort(m); 
4

2 Answers 2

0

You need to make Fruit Comparable

public class Media implements Comparable<Fruit> {
    public int compareTo(Fruit compareFruit) {

        int compareQuantity = ((Media) compareFruit).getSupporto(); 

        //ascending order
        return this.supporto - compareQuantity;

        //descending order
        //return compareQuantity - this.supporto;
    }
...
}

Arrays.sort(m); Some more info : http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

0

You can sort it with your own comparator

    m.sort(new Comparator<Media>() {

        @Override
        public int compare(Media o1, Media o2) {
            return Double.compare(o1.getSupporto(), o2.getSupporto());
        }
    });

this will sort it the way you like. If you need this in many pleaces, i would suggest however creating static comparator like:

private static Comparator<Media> supportoComparator = new Comparator<Media>() {

    @Override
    public int compare(Media o1, Media o2) {
        if (o1 != null && o2 != null) {
            return Double.compare(o1.getSupporto(), o2.getSupporto());
        } else {
            throw new IllegalArgumentException();
        }
    }
};

Above code is in class Media, and then you use it like:

    m.sort(Media.GET_SUPPORTO_COMPARATOR());

and its done

Not the answer you're looking for? Browse other questions tagged or ask your own question.