你可以如何按字母数字顺序排序,忽略非字母数字字符?

huangapple go评论152阅读模式
英文:

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 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.

英文:

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:

  1. Hello
  2. alpha
  3. #%Alpha
  4. --781
  5. hello

After sort:

  1. --781
  2. #%Alpha
  3. Hello
  4. alpha
  5. 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 在排序时删除所有非字母数字字符。

  1. List<String> sortedLines = Files.lines(f.toPath())
  2. .sorted(Comparator.comparing(s -> s.replaceAll("[^a-zA-Z0-9]", "")))
  3. .collect(Collectors.toList());
英文:

You can use String#replaceAll to remove all non-alphanumeric characters when sorting.

  1. List&lt;String&gt; sortedLines = Files.lines(f.toPath())
  2. .sorted(Comparator.comparing(s -&gt; s.replaceAll(&quot;[^a-zA-Z0-9]&quot;, &quot;&quot;)))
  3. .collect(Collectors.toList());

答案2

得分: 0

你也可以使用Collections.sort并定义一个自定义比较器,如下所示:

  1. private static List<String> sort(List<String> inputList){
  2. Collections.sort(inputList, (s1, s2) -> s1.replaceAll("[^a-zA-Z0-9]", "").compareTo(s2.replaceAll("[^a-zA-Z0-9]", "")));
  3. return inputList;
  4. }
英文:

You can also use Collections.sort and define a custom comparator such as following :

  1. private static List&lt;String&gt; sort(List&lt;String&gt; inputList){
  2. Collections.sort(inputList, (s1, s2) -&gt; s1.replaceAll(&quot;[^a-zA-Z0-9]&quot;, &quot;&quot;).compareTo(s2.replaceAll(&quot;[^a-bA-Z0-9]&quot;, &quot;&quot;)));
  3. return inputList;
  4. }

huangapple
  • 本文由 发表于 2020年7月28日 02:56:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63121763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定