英文:
how can I sort alphanumerically ignoring non alphanumeric characters?
问题
I am reading a text file that contains lines with a mix of alphanumeric characters and non-alphanumeric characters. My guess is something like removing the non-alphanumeric characters and sorting then putting them back. I am not sure how to do that or if that's a good way.
Before sort:
Hello
alpha
#%Alpha
--781
hello
After sort:
--781
#%Alpha
Hello
alpha
hello
Current code doing the sorting:
List
This sorts but it is putting #%Alpha before --781, and I am not sure how to remedy this. Any help would be greatly appreciated.
英文:
I am reading a text file that contains lines with a mix of alphanumeric characters and non-alphanumeric characters. My guess is something like removing the non alphanumeric characters and sorting then putting them back I am not sure how to do that or if that's a good way.
Before sort:
Hello
alpha
#%Alpha
--781
hello
After sort:
--781
#%Alpha
Hello
alpha
hello
current code doing the sorting:
List<String> sortedLines = Files.lines(f.toPath()).sorted().collect(Collectors.toList());
This sorts but it is putting #%Alpha before --781 and I am not sure how to remedy this. Any help would be greatly appreciated.
答案1
得分: 3
你可以使用 String#replaceAll
在排序时删除所有非字母数字字符。
List<String> sortedLines = Files.lines(f.toPath())
.sorted(Comparator.comparing(s -> s.replaceAll("[^a-zA-Z0-9]", "")))
.collect(Collectors.toList());
英文:
You can use String#replaceAll
to remove all non-alphanumeric characters when sorting.
List<String> sortedLines = Files.lines(f.toPath())
.sorted(Comparator.comparing(s -> s.replaceAll("[^a-zA-Z0-9]", "")))
.collect(Collectors.toList());
答案2
得分: 0
你也可以使用Collections.sort并定义一个自定义比较器,如下所示:
private static List<String> sort(List<String> inputList){
Collections.sort(inputList, (s1, s2) -> s1.replaceAll("[^a-zA-Z0-9]", "").compareTo(s2.replaceAll("[^a-zA-Z0-9]", "")));
return inputList;
}
英文:
You can also use Collections.sort and define a custom comparator such as following :
private static List<String> sort(List<String> inputList){
Collections.sort(inputList, (s1, s2) -> s1.replaceAll("[^a-zA-Z0-9]", "").compareTo(s2.replaceAll("[^a-bA-Z0-9]", "")));
return inputList;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论