I'm trying to sort an arrayList with a object I created called TextObject. I'd like to sort the arrayList is ascending value, based on a double value stored in the TextObject. I am able to make a comparable for int but struggling with doubles.
Here is my textObject code implementing Comparable:
public class TextObject implements Comparable<TextObject> {
private String line = "";
private int lineNum = 0;
private double score;
/**
* constructor to build TO
* @param lineScore
* @param line
* @param lineNum
*/
TextObject(String line, int lineNum){
//this.lineScore = lineScore;
this.line = line;
this.lineNum = lineNum;
}
/**
* to string method to print it out
*/
@Override
public String toString() {
return "TextObject [line=" + line + ", lineNum=" + lineNum + ", score=" + score + "]";
}
@Override
public double compareTo(TextObject comparestu) {
double compareScore=((TextObject)comparestu).getScore();
return this.score-compareScore;
}
/**
* @return the score
*/
public double getScore() {
return score;
}
}
Then I'm calling the arrayList using Collections.sort from another class:
Collections.sort(st.getLinesArr());
for(TextObject to : st.getLinesArr()){
System.out.println(to);
}
I'd like help with the comparable method.