Java List 根据整数排序,带有多个下划线的情况

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

Java List String sort Based on Integer with multiple underscore's

问题

你好,你可以使用Java 8中的lambda表达式和Comparator来实现这个排序逻辑。以下是你的代码片段的排序实现:

  1. keyValues.sort(Comparator.comparing(s -> Arrays.asList(s.split("_"))
  2. .stream()
  3. .map(Integer::parseInt)
  4. .collect(Collectors.toList())));

这将按照你描述的逻辑对keyValues进行排序。

英文:

Hi All I have arrayList of String datatype but needs to sort like integer with multiple underscore's using Java 8 only ,For your reference I have added sample data, the original data came Liferay 7.1 DDL

  1. List<String> keyValues = new ArrayList<String>();
  2. keyValues.add("100_10_20_1");
  3. keyValues.add("100001");
  4. keyValues.add("100002");
  5. keyValues.add("100002_1");
  6. keyValues.add("100003");
  7. keyValues.add("100");
  8. keyValues.add("100_1");
  9. keyValues.add("100_2");
  10. keyValues.add("100_1_1");
  11. keyValues.add("100_10_20");
  12. keyValues.add("10000001");
  13. keyValues.add("100_10_20_2");

i tried to replace the underscore with 0 but what happens sometimes 100_1 replace _ with 0 then it becomes 10001 but what if i already have 10001 value present in list ? even same thing for empty if i replace _ with "" then it becomes 1001 but what if i already have 1001 then again i am facing issue

Exact logic based on underscore was

  1. 100
  2. 100_1
  3. 100_1_2
  4. /after that only/
  5. 100_2
  6. 100_2_2
  7. /after that/
  8. 103
  9. 104
  10. 105
  11. 10000
  12. /similarly the pattern repeats/
  13. 10000_1
  14. 10000_2
  15. 10000_2_1
  16. 100002

i need the exact output as

  1. 100
  2. 100_1
  3. 100_1_1
  4. 100_2
  5. 100_10_20
  6. 100_10_20_1
  7. 100_10_20_2
  8. 100001
  9. 100002
  10. 100002_1
  11. 100003
  12. 10000001

i tried with replacing underscore with 0 or empty space but there is possiblity of if it had already that number present in list,if i replace with dot and storing as Double then multiple pointer exception will occur

答案1

得分: 4

我创建了一个比较器,然后根据每个字符串具有的最小标记数量进行分割和处理。

  1. static Comparator<String> stringComparator = new Comparator<String>() {
  2. @Override
  3. public int compare(String o1, String o2) {
  4. String[] s1 = o1.split("_");
  5. String[] s2 = o2.split("_");
  6. int minLen = Math.min(s1.length, s2.length);
  7. for (int i=0; i < minLen; i++){
  8. if (!s1[i].equals(s2[i])){
  9. return Integer.parseInt(s1[i]) - Integer.parseInt(s2[i]);
  10. }
  11. }
  12. return o1.compareTo(o2);
  13. }
  14. };

调用方式如下:

  1. List<String> keyValues = new ArrayList<String>();
  2. keyValues.add("100001");
  3. keyValues.add("100002");
  4. keyValues.add("100002_1");
  5. keyValues.add("100003");
  6. keyValues.add("100");
  7. keyValues.add("100_1");
  8. keyValues.add("100_2");
  9. keyValues.add("100_1_1");
  10. keyValues.add("100_10_20");
  11. keyValues.add("10000001");
  12. keyValues.stream().sorted(stringComparator).forEach(System.out::println);

输出结果:

  1. 100
  2. 100_1
  3. 100_1_1
  4. 100_2
  5. 100_10_20
  6. 100001
  7. 100002
  8. 100002_1
  9. 100003
  10. 10000001
英文:

I made a comparator then split the string and process based on the min amount of tokens each string has.

  1. static Comparator&lt;String&gt; stringComparator = new Comparator&lt;String&gt;() {
  2. @Override
  3. public int compare(String o1, String o2) {
  4. String[] s1 = o1.split(&quot;_&quot;);
  5. String[] s2 = o2.split(&quot;_&quot;);
  6. int minLen = Math.min(s1.length, s2.length);
  7. for (int i=0; i &lt; minLen; i++){
  8. if (!s1[i].equals(s2[i])){
  9. return Integer.parseInt(s1[i]) - Integer.parseInt(s2[i]);
  10. // return s1[i].compareTo(s2[i]); //not natural order/sort
  11. }
  12. }
  13. return o1.compareTo(o2);
  14. }
  15. };

Calling it like so:

  1. List&lt;String&gt; keyValues = new ArrayList&lt;String&gt;();
  2. keyValues.add(&quot;100001&quot;);
  3. keyValues.add(&quot;100002&quot;);
  4. keyValues.add(&quot;100002_1&quot;);
  5. keyValues.add(&quot;100003&quot;);
  6. keyValues.add(&quot;100&quot;);
  7. keyValues.add(&quot;100_1&quot;);
  8. keyValues.add(&quot;100_2&quot;);
  9. keyValues.add(&quot;100_1_1&quot;);
  10. keyValues.add(&quot;100_10_20&quot;);
  11. keyValues.add(&quot;10000001&quot;);
  12. keyValues.stream().sorted(stringComparator).forEach(System.out::println);

Gives output:

  1. 100
  2. 100_1
  3. 100_1_1
  4. 100_2
  5. 100_10_20
  6. 100001
  7. 100002
  8. 100002_1
  9. 100003
  10. 10000001

huangapple
  • 本文由 发表于 2023年5月10日 17:57:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217086.html
匿名

发表评论

匿名网友

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

确定