Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按字母顺序排列字符串arrayList,位于[Java]后面的单词后面_Java_File_Sorting_Notepad - Fatal编程技术网

按字母顺序排列字符串arrayList,位于[Java]后面的单词后面

按字母顺序排列字符串arrayList,位于[Java]后面的单词后面,java,file,sorting,notepad,Java,File,Sorting,Notepad,我在记事本中创建了一个图书库,如下所示: Author | Name of the book | Availability? | Readers Code | Return Date ---------------------------------------------------------------------------- J. K. Rowling Harry Potter Yes -

我在记事本中创建了一个图书库,如下所示:

    Author           | Name of the book | Availability? | Readers Code | Return Date

----------------------------------------------------------------------------
    J. K. Rowling      Harry Potter       Yes             -              -
    Mark Sullivan      The Black Book     Yes             -              -
    Margaret Atwood    Rogue              Yes             -              -
我需要按书名的字母顺序来排序。 使用\t分隔单词。 我该怎么做

This is the code that adds new books :
try (BufferedWriter bw = new BufferedWriter(
                        new FileWriter("C:/Users/Name/Desktop/library.txt", true))) {


                    System.out.print("Enter authors name\t");
                    name = sc.nextLine();
                    name = sc.nextLine();
                    author[l] = name;

                    System.out.print("Enter books name\t");
                    name = sc.nextLine();
                    bookname[l] = name;

                    vaiBib[l] = "Yes";
                    BilNr[l] = "-";
                    AtgDat[l] = "-";

                    l++;

                    if ((author[l- 1] != null) && (bookname[l- 1] != null)) {
                        content = bookname[l- 1] + "\t\t" + author[l- 1] + "\t\t" + vaiBib[l- 1]
                                + "\t\t\t" + BilNr[l- 1] + "\t\t" + AtgDat[l- 1];
                        bw.write(content);
                        bw.newLine();

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
用这段代码,我从记事本中的行中创建了链表:

BufferedReader br = new BufferedReader(new FileReader("C:/Users/Name/Desktop/library.txt"));
    ArrayList<String> Line2= new ArrayList<>();
                    while ((line = br.readLine()) != null) {
                        Line2.add(line);
                    }
BufferedReader br=new BufferedReader(新文件阅读器(“C:/Users/Name/Desktop/library.txt”);
ArrayList Line2=新的ArrayList();
而((line=br.readLine())!=null){
行2.添加(行);
}
如果您可以使用Java 8:

Line2.sort(Comparator.comparing(s -> s.substring(s.indexOf('\t') + 1)));

在早期的Java版本中,您需要编写一个
Comparator
类,您可以在Internet上找到许多示例和教程。通过选择
s.substring(s.indexOf('\t')+1))
来比较第一个标签后面的字符串部分的基本思想仍然有效,您只需要对正在比较的两个字符串中的每一个进行比较。

您能帮我们添加代码并编辑您的帖子吗?这使得回答更容易。但请张贴最低限度的必要代码!!