I have read a lot about this in this forum and other fora, but i still cannot get a concrete answer. Anyway i decided to do it like this:
Here is a class to hold a string and an integer:
public class Tuple{
private String token;
private int docID;
public Tuple(String token, int docID) {
this.token = token;
this.docID = docID;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public int getDocID() {
return docID;
}
public void setDocID(int docID) {
this.docID = docID;
}
}
And then, i create an array-list to put these tuples private ArrayList<Tuple> temps = new ArrayList<>();
Then i populate the arraylist like this:
for ( int i = 0; i < numberOfDocs; i++ )
{
Tuple cat = new Tuple(Double.toString(vect[i]),i);
temps.add(cat);
}
Eventually, I am sorting the array like this:
public void sortTmp()
{
Collections.sort(temps, new Comparator<Tuple>()
{
@Override
public int compare(Tuple tr2, Tuple tr1)
{
return tr2.getToken().compareTo(tr1.getToken());
}
});
}
There is some problem with java and doubles and i cannot use directly my double matrix so i have to do Double.toString() . The results are sorted, but not entirely correct because string calculation from double is not very accurate in terms of double number sorting.
Any ideas?
return tr2.getToken().compareTo(tr1.getToken());is wrong when i change everything fromstringtype todoubletype. According to my IDE:double cannot be dereferencedreturn Double.compare( tr2.getToken(), tr1.getToken() );(when changing so that getToken() returnsdouble)