簡體   English   中英

使用模式對 Java 字符串數組進行排序

[英]Sorting a Java String Array with a pattern

我目前正在讀取和寫入文本文件,但我想不出一種排序方式。 我以為我可以按模式排序。 我想按模式(0-9,AZ,az)對 java 字符串數組進行排序。 基本上我想忽略非字母數字字符,用字母前面的數字和小寫字母前面的大寫字母(即0-9、AZ、az)進行排序。 我想刪除只有非字母數字字符的行。

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();

我可能會在最后使用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();

編輯:您當然可以通過快速檢查排序等方式使這項工作更快/更好 - 但通常這是我認為的想法

您在閱讀文件時無法對文件進行排序,在您的情況下需要先完成:

// 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);
}

嘗試這個。

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM