Java List 排序:供应商分数优先,报价总额次之,中标逻辑实现
以下是一个可能的实现:
public class Supplier implements Comparable<Supplier> {
private String name;
private double score;
private double quoteTotal;
private boolean isWinning;
// constructors, getters, setters, etc.
@Override
public int compareTo(Supplier other) {
if (this.score != other.score) {
// sort by score in reverse order
return Double.compare(other.score, this.score);
} else if (this.quoteTotal != other.quoteTotal) {
// if scores are equal, sort by quoteTotal in ascending order
return Double.compare(this.quoteTotal, other.quoteTotal);
} else {
// if both scores and quoteTotals are equal, keep original order
return 0;
}
}
}
public static void sortSuppliers(List<Supplier> suppliers) {
// sort suppliers by score and quoteTotal
Collections.sort(suppliers);
// set isWinning flag for first supplier with score > 0
boolean foundWinner = false;
for (Supplier supplier : suppliers) {
if (supplier.getScore() > 0) {
supplier.setWinning(true);
foundWinner = true;
break;
}
}
// if no supplier has score > 0, set isWinning flag for first supplier
if (!foundWinner && !suppliers.isEmpty()) {
suppliers.get(0).setWinning(true);
}
}
这里定义了一个Supplier类,实现了Comparable接口,用于比较和排序。sortSuppliers方法接受一个List<Supplier>参数,先按照供应商分数进行排序,然后按照报价总额进行升序排序。最后,设置isWinning字段为true的供应商为中标供应商。如果没有供应商的分数大于0,则默认第一个供应商为中标供应商。
原文地址: http://www.cveoy.top/t/topic/ns00 著作权归作者所有。请勿转载和采集!