如何排序使用多个排序标准的ArrayList? [英] How to sort an ArrayList using multiple sorting criteria?
问题描述
我有一个包含引用对象的数组列表。我希望能够通过名称字母顺序排序,通过改变和变化百分比。我如何排序的ArrayList我?
包org.stocktwits.model;的Bean;
进口java.text.DecimalFormat中; 公共类报价实现Serializable { 私有静态最后的serialVersionUID长1L =; 公共字符串符号;
公共字符串名称;
公共字符串的变化;
公共字符串percentChange;
公共字符串打开;
公共字符串daysHigh;
公共字符串daysLow;
公共字符串dividendYield;
公共字符串量;
公共字符串averageDailyVolume;
公共字符串操作;
公共字符串marketCapitalization;
公共字符串yearHigh;
公共字符串yearLow;
公共字符串lastTradePriceOnly;
公共DecimalFormat的DF =新的DecimalFormat(#,###,###,###,###,## 0.00);
公共DecimalFormat的VF =新的DecimalFormat(#,###,###,###,###,## 0); 公共字符串getSymbol(){
返回符号;
}
公共无效setSymbol(字符串符号){
this.symbol =符号;
}
公共字符串的getName(){
返回名称;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
公共字符串getChange(){
返回的变化;
}
公共无效setChange(字符串变化){
如果(change.equals(空)){
this.change =N / A;
}
其他{
浮floatedChange = Float.valueOf(变化);
this.change =(df.format(floatedChange));
}
}
公共字符串getPercentChange(){
返回percentChange;
}
公共无效setPercentChange(字符串percentChange){
如果(percentChange.equals(空))
percentChange =N / A;
其他
this.percentChange = percentChange;
}
公共字符串getOpen(){
返回打开;
}
公共无效就是setOpen(串开路){
如果(open.equals(空))
this.open =N / A;
其他
this.open =开;
}
公共字符串getDaysHigh(){
返回daysHigh;
}
公共无效setDaysHigh(字符串daysHigh){
如果(daysHigh.equals(空))
this.daysHigh =N / A;
其他{
浮floatedDaysHigh = Float.valueOf(daysHigh);
this.daysHigh =(df.format(floatedDaysHigh));
}
}
公共字符串getDaysLow(){
返回daysLow;
}
公共无效setDaysLow(字符串daysLow){
如果(daysLow.equals(空))
this.daysLow =N / A;
其他{
浮floatedDaysLow = Float.valueOf(daysLow);
this.daysLow =(df.format(floatedDaysLow));
}
}
公共字符串getVolume(){
回流量;
}
公共无效setVolume(字符串音量){
如果(volume.equals(空)){
this.volume =N / A;
}
其他{
浮floatedVolume = Float.valueOf(体积);
this.volume =(vf.format(floatedVolume));
}
}
公共字符串getDividendYield(){
返回dividendYield;
}
公共无效setDividendYield(字符串dividendYield){
如果(dividendYield.equals(空))
this.dividendYield =N / A;
其他
this.dividendYield = dividendYield;
}
公共字符串getAverageDailyVolume(){
返回averageDailyVolume;
}
公共无效setAverageDailyVolume(字符串averageDailyVolume){
如果(averageDailyVolume.equals(空)){
this.averageDailyVolume =N / A;
}
其他{
浮floatedAverageDailyVolume = Float.valueOf(averageDailyVolume);
this.averageDailyVolume =(vf.format(floatedAverageDailyVolume));
}
}
公共字符串getPeRatio(){
返回操作;
}
公共无效setPeRatio(字符串操作){
如果(peRatio.equals(空))
this.peRatio =N / A;
其他
this.peRatio =操作;
}
公共字符串getMarketCapitalization(){
返回marketCapitalization;
}
公共无效setMarketCapitalization(字符串marketCapitalization){
如果(marketCapitalization.equals(空))
this.marketCapitalization =N / A;
其他
this.marketCapitalization = marketCapitalization;
}
公共字符串getYearHigh(){
返回yearHigh;
}
公共无效setYearHigh(字符串yearHigh){
如果(yearHigh.equals(空))
this.yearHigh =N / A;
其他
this.yearHigh = yearHigh;
}
公共字符串getYearLow(){
返回yearLow;
}
公共无效setYearLow(字符串yearLow){
如果(yearLow.equals(空))
this.yearLow =N / A;
其他
this.yearLow = yearLow;
} 公共字符串getLastTradePriceOnly(){
返回lastTradePriceOnly;
} 公共无效setLastTradePriceOnly(字符串lastTradePriceOnly){
如果(lastTradePriceOnly.equals(空)){
this.lastTradePriceOnly =N / A;
}
其他{
浮floatedLastTradePriceOnly = Float.valueOf(lastTradePriceOnly);
this.lastTradePriceOnly =(df.format(floatedLastTradePriceOnly));
}
} @覆盖
公众诠释哈希code(){
最终诠释黄金= 31;
INT结果= 1;
结果=黄金*结果+((其他城市== NULL)?0:change.hash code());
结果=黄金*结果
+((daysHigh == NULL)?0:daysHigh.hash code());
结果=黄金*结果+((daysLow == NULL)?0:daysLow.hash code());
结果=总理
*结果
+((lastTradePriceOnly == NULL)?0:lastTradePriceOnly
.hash code());
结果=总理
*结果
+((marketCapitalization == NULL)?0:marketCapitalization
.hash code());
结果=黄金*结果+((名称== NULL)0:name.hash code());
结果=黄金*结果+((开== NULL)?0:open.hash code());
结果=黄金*结果+((操作== NULL)?0:peRatio.hash code());
结果=黄金*结果
+((percentChange == NULL)0:percentChange.hash code());
结果=黄金*结果+((符号== NULL)?0:symbol.hash code());
结果=黄金*结果+((体积== NULL)?0:volume.hash code());
结果=黄金*结果
+((yearHigh == NULL)?0:yearHigh.hash code());
结果=黄金*结果+((yearLow == NULL)?0:yearLow.hash code());
返回结果;
}
@覆盖
公共布尔等于(obj对象){
如果(这种== OBJ)
返回true;
如果(OBJ == NULL)
返回false;
如果(的getClass()!= obj.getClass())
返回false;
报价等=(行情)目标文件;
如果(改== NULL){
如果(other.change!= NULL)
返回false;
}否则如果(!change.equals(other.change))
返回false;
如果(daysHigh == NULL){
如果(other.daysHigh!= NULL)
返回false;
}否则如果(!daysHigh.equals(other.daysHigh))
返回false;
如果(daysLow == NULL){
如果(other.daysLow!= NULL)
返回false;
}否则如果(!daysLow.equals(other.daysLow))
返回false;
如果(lastTradePriceOnly == NULL){
如果(other.lastTradePriceOnly!= NULL)
返回false;
}否则如果(!lastTradePriceOnly.equals(other.lastTradePriceOnly))
返回false;
如果(marketCapitalization == NULL){
如果(other.marketCapitalization!= NULL)
返回false;
}否则如果(!marketCapitalization.equals(other.marketCapitalization))
返回false;
如果(名称== NULL){
如果(other.name!= NULL)
返回false;
}否则如果(!name.equals(other.name))
返回false;
如果(开== NULL){
如果(other.open!= NULL)
返回false;
}否则如果(!open.equals(other.open))
返回false;
如果(操作== NULL){
如果(other.peRatio!= NULL)
返回false;
}否则如果(!peRatio.equals(other.peRatio))
返回false;
如果(percentChange == NULL){
如果(other.percentChange!= NULL)
返回false;
}否则如果(!percentChange.equals(other.percentChange))
返回false;
如果(符号== NULL){
如果(other.symbol!= NULL)
返回false;
}否则如果(!symbol.equals(other.symbol))
返回false;
如果(卷== NULL){
如果(other.volume!= NULL)
返回false;
}否则如果(!volume.equals(other.volume))
返回false;
如果(yearHigh == NULL){
如果(other.yearHigh!= NULL)
返回false;
}否则如果(!yearHigh.equals(other.yearHigh))
返回false;
如果(yearLow == NULL){
如果(other.yearLow!= NULL)
返回false;
}否则如果(!yearLow.equals(other.yearLow))
返回false;
返回true;
}
}
创建一个适当的<一个href=\"http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html\"><$c$c>Comparator$c$c>将根据您的所需条件比较两个项目。然后使用<一个href=\"http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29\"><$c$c>Collections.sort()$c$c>在你的ArrayList
如果以后你要根据不同的标准进行排序,通话 Collections.sort()
用不同的比较再次
。
I have an array list that contains Quote objects. I want to be able to sort alphabetically by name, by change, and by percent change. How can I sort my 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() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getChange() {
return change;
}
public void setChange(String change) {
if(change.equals("null")){
this.change = "N/A";
}
else{
float floatedChange = Float.valueOf(change);
this.change = (df.format(floatedChange));
}
}
public String getPercentChange() {
return percentChange;
}
public void setPercentChange(String percentChange) {
if(percentChange.equals("null"))
percentChange = "N/A";
else
this.percentChange = percentChange;
}
public String getOpen() {
return open;
}
public void setOpen(String open) {
if(open.equals("null"))
this.open = "N/A";
else
this.open = open;
}
public String getDaysHigh() {
return daysHigh;
}
public void setDaysHigh(String daysHigh) {
if(daysHigh.equals("null"))
this.daysHigh = "N/A";
else{
float floatedDaysHigh = Float.valueOf(daysHigh);
this.daysHigh = (df.format(floatedDaysHigh));
}
}
public String getDaysLow() {
return daysLow;
}
public void setDaysLow(String daysLow) {
if(daysLow.equals("null"))
this.daysLow = "N/A";
else{
float floatedDaysLow = Float.valueOf(daysLow);
this.daysLow = (df.format(floatedDaysLow));
}
}
public String getVolume() {
return volume;
}
public void setVolume(String volume) {
if(volume.equals("null")){
this.volume = "N/A";
}
else{
float floatedVolume = Float.valueOf(volume);
this.volume = (vf.format(floatedVolume));
}
}
public String getDividendYield() {
return dividendYield;
}
public void setDividendYield(String dividendYield) {
if(dividendYield.equals("null"))
this.dividendYield = "N/A";
else
this.dividendYield = dividendYield;
}
public String getAverageDailyVolume() {
return averageDailyVolume;
}
public void setAverageDailyVolume(String averageDailyVolume) {
if(averageDailyVolume.equals("null")){
this.averageDailyVolume = "N/A";
}
else{
float floatedAverageDailyVolume = Float.valueOf(averageDailyVolume);
this.averageDailyVolume = (vf.format(floatedAverageDailyVolume));
}
}
public String getPeRatio() {
return peRatio;
}
public void setPeRatio(String peRatio) {
if(peRatio.equals("null"))
this.peRatio = "N/A";
else
this.peRatio = peRatio;
}
public String getMarketCapitalization() {
return marketCapitalization;
}
public void setMarketCapitalization(String marketCapitalization) {
if(marketCapitalization.equals("null"))
this.marketCapitalization = "N/A";
else
this.marketCapitalization = marketCapitalization;
}
public String getYearHigh() {
return yearHigh;
}
public void setYearHigh(String yearHigh) {
if(yearHigh.equals("null"))
this.yearHigh = "N/A";
else
this.yearHigh = yearHigh;
}
public String getYearLow() {
return yearLow;
}
public void setYearLow(String yearLow) {
if(yearLow.equals("null"))
this.yearLow = "N/A";
else
this.yearLow = yearLow;
}
public String getLastTradePriceOnly() {
return lastTradePriceOnly;
}
public void setLastTradePriceOnly(String lastTradePriceOnly) {
if(lastTradePriceOnly.equals("null")){
this.lastTradePriceOnly = "N/A";
}
else{
float floatedLastTradePriceOnly = Float.valueOf(lastTradePriceOnly);
this.lastTradePriceOnly = (df.format(floatedLastTradePriceOnly));
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((change == null) ? 0 : change.hashCode());
result = prime * result
+ ((daysHigh == null) ? 0 : daysHigh.hashCode());
result = prime * result + ((daysLow == null) ? 0 : daysLow.hashCode());
result = prime
* result
+ ((lastTradePriceOnly == null) ? 0 : lastTradePriceOnly
.hashCode());
result = prime
* result
+ ((marketCapitalization == null) ? 0 : marketCapitalization
.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((open == null) ? 0 : open.hashCode());
result = prime * result + ((peRatio == null) ? 0 : peRatio.hashCode());
result = prime * result
+ ((percentChange == null) ? 0 : percentChange.hashCode());
result = prime * result + ((symbol == null) ? 0 : symbol.hashCode());
result = prime * result + ((volume == null) ? 0 : volume.hashCode());
result = prime * result
+ ((yearHigh == null) ? 0 : yearHigh.hashCode());
result = prime * result + ((yearLow == null) ? 0 : yearLow.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Quote other = (Quote) obj;
if (change == null) {
if (other.change != null)
return false;
} else if (!change.equals(other.change))
return false;
if (daysHigh == null) {
if (other.daysHigh != null)
return false;
} else if (!daysHigh.equals(other.daysHigh))
return false;
if (daysLow == null) {
if (other.daysLow != null)
return false;
} else if (!daysLow.equals(other.daysLow))
return false;
if (lastTradePriceOnly == null) {
if (other.lastTradePriceOnly != null)
return false;
} else if (!lastTradePriceOnly.equals(other.lastTradePriceOnly))
return false;
if (marketCapitalization == null) {
if (other.marketCapitalization != null)
return false;
} else if (!marketCapitalization.equals(other.marketCapitalization))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (open == null) {
if (other.open != null)
return false;
} else if (!open.equals(other.open))
return false;
if (peRatio == null) {
if (other.peRatio != null)
return false;
} else if (!peRatio.equals(other.peRatio))
return false;
if (percentChange == null) {
if (other.percentChange != null)
return false;
} else if (!percentChange.equals(other.percentChange))
return false;
if (symbol == null) {
if (other.symbol != null)
return false;
} else if (!symbol.equals(other.symbol))
return false;
if (volume == null) {
if (other.volume != null)
return false;
} else if (!volume.equals(other.volume))
return false;
if (yearHigh == null) {
if (other.yearHigh != null)
return false;
} else if (!yearHigh.equals(other.yearHigh))
return false;
if (yearLow == null) {
if (other.yearLow != null)
return false;
} else if (!yearLow.equals(other.yearLow))
return false;
return true;
}
}
Create an appropiate Comparator
that will compare two items according to your desired criteria. Then use Collections.sort()
on your ArrayList.
If at a later time you want to sort by different criteria, call Collections.sort()
again with a different Comparator
.
这篇关于如何排序使用多个排序标准的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!