[英]Sorting a Java String Array with a pattern
I am currently reading and writing to a text file and I cant figure out a way to sort.我目前正在读取和写入文本文件,但我想不出一种排序方式。 I thought I would be able to sort by pattern.
我以为我可以按模式排序。 I would like to sort a java string array by the pattern (0-9, AZ, az).
我想按模式(0-9,AZ,az)对 java 字符串数组进行排序。 Basically I would like to ignore non-alphanumeric characters, sort with numbers preceding letters, and capital letters preceding lowercase letters (ie, 0-9, AZ, az).
基本上我想忽略非字母数字字符,用字母前面的数字和小写字母前面的大写字母(即0-9、AZ、az)进行排序。 I would like to remove lines that only have non-alphanumeric characters.
我想删除只有非字母数字字符的行。
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();
I would likely use something like Collections.sort()
at the end and a simple check in the loop:我可能会在最后使用
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();
EDIT: You can certainly make this work faster/better by quick-checking perhaps in the sort, etc - but generally this is the idea I think编辑:您当然可以通过快速检查排序等方式使这项工作更快/更好 - 但通常这是我认为的想法
You can't sort the file while you are reading it, in your case it needs to be done before:您在阅读文件时无法对文件进行排序,在您的情况下需要先完成:
// 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);
}
Try this.尝试这个。
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.