0
如何对PriorityQueue进行排序,使所有具有“true”值的bool对象始终位于顶部,其他位置的底部为false值,并按日期。如何在java中使用布尔和日期对PriorityQueue进行排序
import java.text.DateFormat;
import java.util.*;
import static java.text.DateFormat.*;
import static java.util.Locale.*;
public class Patient implements Comparable<Patient> {
private String name;
private Date termin;
private boolean emergency;
Patient(String name, Date termin, boolean emergency) {
this.name = name;
this.termin = termin;
this.emergency = emergency;
}
public String getName(){
return name;
}
@Override
public String toString()
{
if (emergency)
{
return "** Notfall! ** \t "+ this.name;
}
else
{
DateFormat df = getDateInstance(SHORT, GERMANY);
DateFormat tf = DateFormat.getTimeInstance(SHORT,GERMANY);
return df.format(this.termin)+" "+tf.format(this.termin)+" \t "+this.name;
}
}
@Override
public int compareTo(Patient o) {
int i = 0;/*
if (this.equals(null)|| o.equals(null))
{
i= 0;
}
if (this.emergency == o.emergency)
{
if(this.termin.getTime() < o.termin.getTime()){
i=1;
}else{
i= -1;
}
}
if (this.emergency || o.emergency)
{
i= -1;
}
return i;*/
}
}
结果: (不正确排序)
Warteschlange:
[email protected]
Nächster Patient: ** Notfall! ** Ralph
Nächster Patient: ** Notfall! ** anonym
Nächster Patient: 21.10.11 09:15 Antonia Adam
Nächster Patient: 21.10.11 08:30 Cäcilia Chlum
Nächster Patient: 21.10.11 09:30 Bert Bauer
结果应该是这样的:
Warteschlange:
[email protected]
Nächster Patient: ** Notfall! ** Ralph
Nächster Patient: ** Notfall! ** anonym
Nächster Patient: 21.10.11 08:30 Cäcilia Chlum
Nächster Patient: 21.10.11 09:15 Antonia Adam
Nächster Patient: 21.10.11 09:30 Bert Bauer