我有一个以Person
多个属性命名的类,例如:
public class Person {
private int id;
private String name, address;
// Many more properties.
}
Run Code Online (Sandbox Code Playgroud)
很多Person
对象存储在一个ArrayList<Person>
.我想通过多个排序参数对此列表进行排序,并且不时有所不同.例如,我可能有一次想要按name
升序然后address
降序排序,而另一次只能通过id
降序排序.
而且我不想创建自己的排序方法(即,我想使用Collections.sort(personList, someComparator)
.实现这一目标的最优雅的解决方案是什么?
我有一个包含Quote对象的数组列表.我希望能够按名称,更改和百分比更改按字母顺序排序.我怎样才能对我的arraylist进行排序?
package org.stocktwits.model;
import java.io.Serializable;
import java.text.DecimalFormat;
public class Quote implements Serializable {
private static final long serialVersionUID = 1L;
public String symbol;
public String name;
public String change;
public String percentChange;
public String open;
public String daysHigh;
public String daysLow;
public String dividendYield;
public String volume;
public String averageDailyVolume;
public String peRatio;
public String marketCapitalization;
public String yearHigh;
public String yearLow;
public String lastTradePriceOnly;
public DecimalFormat df = new DecimalFormat("#,###,###,###,###,##0.00");
public DecimalFormat vf = new DecimalFormat("#,###,###,###,###,##0");
public String getSymbol() …
Run Code Online (Sandbox Code Playgroud) 我正在追踪一个包含许多有用的Comparator实现的化石库,它有许多可以随意使用的无聊的比较器.
Apache commons比较器
还有许多其他有用的可重用可能性.
@SPF我已经包含了一些伪代码,它们显示了如何在一次传递中进行规范化和比较而不创建任何临时字符串等.虽然它未经测试并且可能无法工作,但是进行快速比较并不需要太多.
while {
while
get next char from $string1
if none left then
$string1 > $string2 return +1;
get next char from $string1
increase $index1
if previous was whitespace and this is whitespace then
continue;
end if
end while
while
get next char from $string2
if none left then
$string2 > $string1 return +1;
get next char from $string2
increase $index2
if …
Run Code Online (Sandbox Code Playgroud)