推移的ではありません。
要素の線形順序付けはできません。
例による証明。私は小文字で線形順序や名前のため<
を使用する単純化するために
c.par2
b.notpar2
a.par2
:
あなたは小文字で名前を持つ3つのDownloadFile
S(c
、b
、a
)を持っていると言います。
c.par2 < b.notpar2
およびb.notpar2 < a.par2
ですが、c.par2 < a.par2
ではありません。
この関係はtransitiveではありません。ロジックで
...それは次のようになります:
cRb
とbRa
、それはcRa
ことは事実ではありません。私はこのような何かのために行くだろう
あなたがしなければならないのは、直線的にあなたのファイルを注文する方法を答えることです...
:それは返したため、最後に
if(aMethodOnThis < aMethodOnOther) {
return -1; //or 1
}
if(aCompletelyDifferentCriterium) {
//...
}
return 0; //or return thisFileName.compareTo(otherFileName);
return 0
は、非常に重要です区別できないファイルの場合その場合
:
public class DownloadFile implements Comparable<DownloadFile>{
String filename;
DownloadFile(String filename) {
this.filename = filename;
}
public String getFilename() {
return this.filename;
}
@Override
public String toString() {
return this.getFilename();
}
@Override
public int compareTo(DownloadFile downloadFile) {
String thisStr = this.filename.toLowerCase();
String oStr = downloadFile.getFilename().toLowerCase();
if(thisStr.endsWith(".rar")) {
if(!oStr.endsWith(".rar"))
return -1;
}
if(oStr.endsWith(".rar")) {
if(!thisStr.endsWith(".rar"))
return 1;
}
if(thisStr.matches(".*\\.r[0-9]{2,}$")) {
if(!oStr.matches(".*\\.r[0-9]{2,}$"))
return -1;
}
if(oStr.matches(".*\\.r[0-9]{2,}$")) {
if(!thisStr.matches(".*\\.r[0-9]{2,}$"))
return 1;
}
if(thisStr.endsWith(".par2")) {
if(!oStr.endsWith(".par2"))
return -1;
}
if(oStr.endsWith(".par2")) {
if(!thisStr.endsWith(".par2"))
return 1;
}
return thisStr.compareTo(oStr);
}
public static void main(String[] args) {
List<DownloadFile> fileList = new ArrayList<>();
fileList.add(new DownloadFile("a.rar"));
fileList.add(new DownloadFile("b.rar"));
fileList.add(new DownloadFile("a.r01"));
fileList.add(new DownloadFile("b.r01"));
fileList.add(new DownloadFile("a.r10"));
fileList.add(new DownloadFile("b.r10"));
fileList.add(new DownloadFile("a.par2"));
fileList.add(new DownloadFile("b.par2"));
fileList.add(new DownloadFile("a.other"));
fileList.add(new DownloadFile("b.other"));
Collections.shuffle(fileList);
Collections.sort(fileList);
System.out.println(fileList);
}
}
のJava 8からPredicate<String>
それを短くするには便利です;)
@Override
public int compareTo(DownloadFile downloadFile) {
String thisStr = this.filename.toLowerCase();
String oStr = downloadFile.getFilename().toLowerCase();
List<Predicate<String>> conditionList = new ArrayList<>();
conditionList.add(s -> s.endsWith(".rar"));
conditionList.add(s -> s.matches(".*\\.r[0-9]{2,}$"));
conditionList.add(s -> s.endsWith(".par2"));
for(Predicate<String> condition : conditionList) {
int orderForCondition =
conditionHelper(thisStr, oStr, condition);
if(orderForCondition != 0)
return orderForCondition;
}
return thisStr.compareTo(oStr);
}
private int conditionHelper(String firstStr, String secondStr,
Predicate<String> condition) {
if(condition.test(firstStr))
if(!condition.test(secondStr))
return -1;
if(condition.test(secondStr))
if(!condition.test(firstStr))
return 1;
return 0;
}
まず第一に、あなたは、このエラーの最も可能性の高い原因の一つを知っていますか? http://stackoverflow.com/a/8327575/1743880 – Tunaki
".r02"エンディングのファイルは、最後に ".par"が付いたファイルの前に来る必要がありますか? –
@GrzegorzGórkiewicz最後に ".par2"を付けた人の前にいるのですか?はい、それは正しいです。 – Matthias