我需要按字符串的长度对LinkedList的字符串进行排序,但是希望保持相同长度字符串的顺序(不按字典顺序排序).
样本输入:
this
is
just
a
test
Run Code Online (Sandbox Code Playgroud)
样本输出:
a
is
this
just
test
Run Code Online (Sandbox Code Playgroud)
我试图用一个Comparable<LinkedList<String>>和一个compareTo方法做这个,但我没有得到正确的输出(我仍然按字典顺序排序)
public class Q3_sorting implements Comparable<LinkedList<String>> {
Scanner keyboardScanner = null;
LinkedList<String> fileList = new LinkedList<String>();
Run Code Online (Sandbox Code Playgroud)
// [...]这里有一些代码
public int compareTo(LinkedList<String> o) {
// TODO Auto-generated method stub
o = fileList;
for (int i = 0; i < fileList.size() -1; i++) {
if (fileList.get(i).length() == o.get(i+1).length()) {
return 0;
}
if (fileList.get(i).length() > o.get(i+1).length()) {
return -1;
}
if (fileList.get(i).length() < o.get(i+1).length()) { …Run Code Online (Sandbox Code Playgroud)