0
我有一個任務,我需要從文本文件中讀取文件,並將行添加到列表中,按照長度對其進行排序並打印出來。我的問題是,Coolections.sort()按字母順序和長度對它進行排序,我只需要按長度對它進行排序,這裏是我的代碼。從文本文件讀取,然後對列表進行排序
import java.util.*;
import java.io.*;
public class Question3
{
public static void main(String [] args) throws IOException
{
Scanner input = new Scanner(System.in);
String path = input.nextLine();
Scanner scanner = new Scanner(new File(path));
List<String> lines = new ArrayList<String>();
while(scanner.hasNext())
{
lines.add(scanner.next());
}
Collections.sort(lines);
System.out.println(lines);
}
}