Sorting with 2 parameters

Hi all

I am looking to sort an ArrayList with a comparator. I first ordered by hitChi which is from the .getHitChi method. However, hitChi is not unique and duplicates are present. I cannot eliminate these duplicates since they contain other important data.

So is this the correct way to sort using a second parameter, a unique code called hitID or am I likely to get some weird results ?

By the way, I can't sort by HitID the first time around since hitChi is meaningful!

Cheers

Colin

Collections.sort(hitsList,new Comparator<Hit>(){

publicint compare(Hit hit1, Hit hit2){

if (hit2.getHitChi() - hit1.getHitChi() !=0 ){

return hit2.getHitChi() - hit1.getHitChi();

}

else{

return hit2.getHitID() - hit1.getHitID();

}

}

});

[1273 byte] By [colin122a] at [2007-11-27 4:19:28]
# 1

> ...

> So is this the correct way to sort using a second

> parameter, a unique code called hitID or am I likely

> to get some weird results ?

If your Chi and ID values are not too big, you should be OK.

If they can get large, be carefull for overflows!

int chi1 = -1073741824;

int chi2 = 1073741825;

System.out.println("chi1 - chi2 = "+chi1+" - "+chi2+" = "+(chi1-chi2));

prometheuzza at 2007-7-12 9:26:24 > top of Java-index,Core,Core APIs...
# 2
Cheers for your replyThe values weren't too large, and this appears to have worked fineTaColin
colin122a at 2007-7-12 9:26:24 > top of Java-index,Core,Core APIs...