Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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/sorting/2.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
Java 使用Collections.sort进行比较_Java_Sorting_Collections_Comparator - Fatal编程技术网

Java 使用Collections.sort进行比较

Java 使用Collections.sort进行比较,java,sorting,collections,comparator,Java,Sorting,Collections,Comparator,我尝试对一个文本文件进行排序,该文件由制表符分隔,并尝试按第三个字段className进行排序。然后我将在一张表中显示它。我写了以下部分,但排序不正确。你知道我错在哪里吗 public void sortFile(){ BufferedReader reader = null; BufferedWriter writer = null; //Create an ArrayList object to hold the lines of input file

我尝试对一个文本文件进行排序,该文件由制表符分隔,并尝试按第三个字段className进行排序。然后我将在一张表中显示它。我写了以下部分,但排序不正确。你知道我错在哪里吗

    public void sortFile(){
    BufferedReader reader = null; 
    BufferedWriter writer = null;

    //Create an ArrayList object to hold the lines of input file
    ArrayList<String> lines = new ArrayList<>();


    try{
        //Creating BufferedReader object to read the input file
        reader = new BufferedReader(new FileReader("pupilInfo.txt"));
        //Reading all the lines of input file one by one and adding them into ArrayList
        String currentLine = reader.readLine();
        while (currentLine != null){
            lines.add(currentLine);
            currentLine = reader.readLine();
        } 


            Collections.sort(lines, (String s1, String s2) -> {
            s1 = lines.get(0);
            s2 = lines.get(1);
            String [] line1 = s1.split("\t");
            String [] line2 = s2.split("\t");
            String classNameLine1 = line1[2];
            String classNameLine2 = line2[2];
            System.out.println("classname1=" + classNameLine1);
            System.out.println("classname2=" + classNameLine2);
            int sComp = classNameLine1.compareTo(classNameLine2);
            return sComp;
        });
        //Creating BufferedWriter object to write into output temp file
        writer = new BufferedWriter(new FileWriter("pupilSortTemp.txt"));
        //Writing sorted lines into output file
        for (String line : lines){
            writer.write(line);
            writer.newLine();
        }            
    }catch (IOException e){
    }
    finally{
    //Closing the resources
        try{
            if (reader != null){
                reader.close();
            }
            if(writer != null){
                writer.close();
            }
        }catch (IOException e){
        }
    }  

}

我用comparator试过了。为了简单起见,我的源文件如下

    public void sortFile(){
    BufferedReader reader = null; 
    BufferedWriter writer = null;

    //Create an ArrayList object to hold the lines of input file
    ArrayList<String> lines = new ArrayList<>();


    try{
        //Creating BufferedReader object to read the input file
        reader = new BufferedReader(new FileReader("pupilInfo.txt"));
        //Reading all the lines of input file one by one and adding them into ArrayList
        String currentLine = reader.readLine();
        while (currentLine != null){
            lines.add(currentLine);
            currentLine = reader.readLine();
        } 


            Collections.sort(lines, (String s1, String s2) -> {
            s1 = lines.get(0);
            s2 = lines.get(1);
            String [] line1 = s1.split("\t");
            String [] line2 = s2.split("\t");
            String classNameLine1 = line1[2];
            String classNameLine2 = line2[2];
            System.out.println("classname1=" + classNameLine1);
            System.out.println("classname2=" + classNameLine2);
            int sComp = classNameLine1.compareTo(classNameLine2);
            return sComp;
        });
        //Creating BufferedWriter object to write into output temp file
        writer = new BufferedWriter(new FileWriter("pupilSortTemp.txt"));
        //Writing sorted lines into output file
        for (String line : lines){
            writer.write(line);
            writer.newLine();
        }            
    }catch (IOException e){
    }
    finally{
    //Closing the resources
        try{
            if (reader != null){
                reader.close();
            }
            if(writer != null){
                writer.close();
            }
        }catch (IOException e){
        }
    }  

}
pippo;pluto;paperino
casa;terra;cortile
primo;secondo;terzo

Comparator<String> comparator = new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    return o1.split(";")[2].compareTo(o2.split(";")[2]);

                }
            };

            lines.sort(comparator);

在第三栏排序

s1=lines.get0;s2=lines.get1;-你到底在干什么?如果删除@user2357112识别的两行,这将是解决问题的良好开端。传递给Collections.sort的比较器已在要从ArrayList进行比较的字符串中传递,因此不需要手动设置它们。此外,您实际上正在覆盖传递给比较器的正确值。我对这一点不熟悉。我取出了上面提到的那几行。谢谢你的评论。非常感谢。我要去一个初学者论坛。我想也许我会在这里得到一些帮助。谢谢你友好地回答我的问题。这个选项适合我。
    public void sortFile(){
    BufferedReader reader = null; 
    BufferedWriter writer = null;

    //Create an ArrayList object to hold the lines of input file
    ArrayList<String> lines = new ArrayList<>();


    try{
        //Creating BufferedReader object to read the input file
        reader = new BufferedReader(new FileReader("pupilInfo.txt"));
        //Reading all the lines of input file one by one and adding them into ArrayList
        String currentLine = reader.readLine();
        while (currentLine != null){
            lines.add(currentLine);
            currentLine = reader.readLine();
        } 


            Collections.sort(lines, (String s1, String s2) -> {
            s1 = lines.get(0);
            s2 = lines.get(1);
            String [] line1 = s1.split("\t");
            String [] line2 = s2.split("\t");
            String classNameLine1 = line1[2];
            String classNameLine2 = line2[2];
            System.out.println("classname1=" + classNameLine1);
            System.out.println("classname2=" + classNameLine2);
            int sComp = classNameLine1.compareTo(classNameLine2);
            return sComp;
        });
        //Creating BufferedWriter object to write into output temp file
        writer = new BufferedWriter(new FileWriter("pupilSortTemp.txt"));
        //Writing sorted lines into output file
        for (String line : lines){
            writer.write(line);
            writer.newLine();
        }            
    }catch (IOException e){
    }
    finally{
    //Closing the resources
        try{
            if (reader != null){
                reader.close();
            }
            if(writer != null){
                writer.close();
            }
        }catch (IOException e){
        }
    }  

}