タイムゾーン値を含む文字列値をソートしたいのですが、これは私がComparableインターフェイスを実装しているためです。文字列値のタイムゾーンソート
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class TimeZoneModel implements Comparable {
private Integer id;
private String value;
public int getId() {
return id;
}
public String getValue() {
return value;
}
public void setId(Integer id) {
this.id = id;
}
public void setValue(String value) {
value = value;
}
public TimeZoneModel(String value){
this.value= value;
}
public int compareTo(Object object) {
TimeZoneModel timezoneModel= null;
if(object instanceof TimeZoneModel){
timezoneModel=(TimeZoneModel)object;
}
return /*value.compareTo*/((timezoneModel.getValue()!=null?timezoneModel.getValue():"")).compareTo(value);
}
public static void main(String args[]){
TimeZoneModel timezoneModel = new TimeZoneModel("+01:00");
TimeZoneModel timezoneModel1 = new TimeZoneModel("+02:30");
TimeZoneModel timezoneModel2 = new TimeZoneModel("-01:00");
TimeZoneModel timezoneModel3 = new TimeZoneModel("-11:00");
TimeZoneModel timezoneModel4 = new TimeZoneModel("+05:00");
List<TimeZoneModel> timeZoneModelList = new ArrayList<TimeZoneModel>();
timeZoneModelList.add(timezoneModel);
timeZoneModelList.add(timezoneModel1);
timeZoneModelList.add(timezoneModel2);
timeZoneModelList.add(timezoneModel3);
timeZoneModelList.add(timezoneModel4);
Collections.sort(timeZoneModelList);
for(TimeZoneModel timezoneModelw : timeZoneModelList){
System.out.println(timezoneModelw.getValue());
}
}
}
上記のプログラムでは、値のベースでカスタムソートを行いたいと考えています。私はそれに匹敵するインターフェイスを使用しました。
出力私は取得しています:
-11:00
-01:00
+05:00
+02:30
+01:00
予想される出力は次のようになります。
-11:00
-01:00
+01:00
+02:30
+05:00
私は、変換を整数に文字列をスキップしたいです。整数の会話は私の最後の選択肢です。