【问题标题】:Sorting a Java String Array with a pattern使用模式对 Java 字符串数组进行排序
【发布时间】:2025-11-21 21:10:02
【问题描述】:

我目前正在读取和写入文本文件,但我想不出一种排序方式。我以为我可以按模式排序。我想按模式(0-9,A-Z,a-z)对 java 字符串数组进行排序。基本上我想忽略非字母数字字符,用字母前面的数字和小写字母前面的大写字母(即 0-9、A-Z、a-z)进行排序。我想删除只有非字母数字字符的行。

File f1 = new File(fp);
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
List<String> lines = new ArrayList<String>();
String line;
while ((line = br.readLine()) != null) {
    count++;
    // SORT GOES HERE

    if (line.contains(sx)) {
        line = line.replace(line, "");
    }

    if (yint > 0 && !line.isBlank()) {
        line = line.substring(yint);
    }

    if(!line.isBlank()){
        line = line.replace(line, count + " " + line + "\n");
        lines.add(line);
    } else {
        lines.add(line);
    }
}
fr.close();
br.close();

FileWriter fw = new FileWriter(f1);
BufferedWriter out = new BufferedWriter(fw);
for(String s : lines)
    out.write(s);
out.flush();
out.close();

【问题讨论】:

标签: java string file sorting


【解决方案1】:

我可能会在最后使用Collections.sort() 之类的东西,并在循环中进行简单的检查:

    File f1 = new File(fp);
    FileReader fr = new FileReader(f1);
    BufferedReader br = new BufferedReader(fr);
    List<String> lines = new ArrayList<String>();
    String line;
    while ((line = br.readLine()) != null) {
        count++;

        if (!line.matches("[a-zA-Z0-9]+")) {
            continue;
        }

        if (line.contains(sx)) {
            line = line.replace(line, "");
        }

        if (yint > 0 && !line.isBlank()) {
            line = line.substring(yint);
        }

        if(!line.isBlank()){
            line = line.replace(line, count + " " + line + "\n");
            lines.add(line);
        } else {
            lines.add(line);
        }
    }
    fr.close();
    br.close();

    Collections.sort(lines, (a, b) -> {
        String aNum = a.replaceAll("[^a-zA-Z0-9]", "");
        String bNum = b.replaceAll("[^a-zA-Z0-9]", "");
        return a.compareTo(b);
    });

    FileWriter fw = new FileWriter(f1);
    BufferedWriter out = new BufferedWriter(fw);
    for(String s : lines)
        out.write(s);
    out.flush();
    out.close();

编辑:您当然可以通过快速检查排序等方式使这项工作更快/更好 - 但通常这是我认为的想法

【讨论】:

  • 他在每行前面添加一个唯一的数字,最后排序没有意义。
  • 嗯,很有趣,谢谢!但是是的,marc 是正确的。
【解决方案2】:

您在阅读文件时无法对文件进行排序,在您的情况下需要先完成:

// Sort the file
Path initialFile = Paths.get("myFile.txt");
List<String> sortedLines = Files.lines(initialFile)
        .sorted()
        .collect(Collectors.toList());

// Process the sorted lines
List<String> lines = new ArrayList<String>();
int count=0;
for(String line : sortedLines) {
    System.out.println("l: "+line);
    count++;

    if (line.contains(sx)) {
        line = line.replace(line, "");
    }

    if (yint > 0 && !line.isEmpty()) {
        line = line.substring(yint);
    }

    if (!line.isEmpty()) {
        System.out.println("line:"+line);
        line = line.replace(line, count + " " + line + "\n");
        System.out.println("new line:"+line);
        lines.add(line);
    } else {
        System.out.println("add:"+line);
        lines.add(line);
    }
}
// Write output file
File f1 = new File("myFile.txt");
try(BufferedWriter out = new BufferedWriter( new FileWriter(f1))){
    for (String s : lines)
        out.write(s);
}

【讨论】:

    【解决方案3】:

    试试这个。

    static void sortByAlphaNumeric(String inFile, String outFile) throws IOException {
        List<String> lines = Files.readAllLines(Paths.get(inFile)).stream()
            .map(line -> new Object() {
                String sortKey = line.replaceAll("[^0-9A-Za-z]", "");
                String originalLine = line;
            })
            .filter(obj -> !obj.sortKey.equals(""))
            .sorted(Comparator.comparing(obj -> obj.sortKey))
            .map(obj -> obj.originalLine)
            .collect(Collectors.toList());
        Files.write(Paths.get(outFile), lines);
    }
    

    【讨论】:

      最近更新 更多