Java对一个包含字符串(混合了数字和字符)的数组进行排序。

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

Java sort an array of strings (mixed with numbers and charcter)

问题

我正在Java中对一个数组进行排序,我的输入是{"a3","a2","a11","b1","b2","b3","c3","c13","c2"},我希望输出是{"a2","a3","a11","b1","b2","b3","c2","c3","c13"}

以下是我在下面的代码中所做的,但没有返回正确的结果,任何建议/代码示例将不胜感激:

import java.util.Arrays;  

public class Main
{
    public static void main(String[] args) {
        String[] var = {"a3", "a2", "a11", "b1", "b2", "b3", "c3", "c13", "c2"};  
        Arrays.sort(var, (s1, s2) -> {
            String[] parts1 = s1.split("(?<=\\D)(?=\\d)");
            String[] parts2 = s2.split("(?<=\\D)(?=\\d)");
            int compare = parts1[0].compareTo(parts2[0]);
            if (compare == 0) {
                if (parts1.length > 1 && parts2.length > 1) {
                    return Integer.compare(Integer.parseInt(parts1[1]), Integer.parseInt(parts2[1]));
                }
            }
            return compare;
        });  
        System.out.println(Arrays.toString(var));  
    }
}

从上面的代码中,我得到的输出是[a2, a3, a11, b1, b2, b3, c2, c3, c13]

英文:

I'm sorting an array in Java, my inputs are {"a3", "a2", "a11", "b1", "b2", "b3", "c3", "c13", "c2"} and I want output {"a2", "a3", "a11", "b1", "b2", "b3", "c2", "c3", "c13"}

What I'm doing in below not returning proper results, any suggestions/code example appreciated

import java.util.Arrays;  

public class Main
{
    public static void main(String[] args) {
        String[] var = {&quot;a3&quot;, &quot;a2&quot;, &quot;a11&quot;, &quot;b1&quot;, &quot;b2&quot;, &quot;b3&quot;, &quot;c3&quot;,  &quot;c13&quot;, &quot;c2&quot;};  
        Arrays.sort(var);  
        System.out.println(Arrays.toString(var));  
	}
}

From above code I'm getting output [a11, a2, a3, b1, b2, b3, c13, c2, c3]

答案1

得分: 1

这里有一个相对简单的方法。然而,对于像这样的情况,我会创建一个类来分别保存字母部分和数字部分,并编写一个比较器。在打印这些对象时,我只需使 toString() 返回原始值。

这是基础方法。它不会检查格式错误的字符串。

String[] var = {"a3", "a2", "a11", "b1", "b2", "b3", "c3", "c13", "c2"};

Comparator<String> comp = (a,b) -> {
    // 在最后一个字符和第一个数字之间分割字符串
    String[] v1 = a.split("(?<![\\d])(?=\\d)");
    String[] v2 = b.split("(?<![\\d])(?=\\d)");
      
    // 将每个整数部分转换为整数。
    int n1 = Integer.valueOf(v1[1]);
    int n2 = Integer.valueOf(v2[1]);

    // 比较两者并获取结果
    int r1 = v1[0].compareTo(v2[0]);
    int r2 = Integer.compare(n1, n2);

    // 首先根据 r1(字符串)排序。如果它们相等(r1 == 0)
    // 则根据比较整数的结果进行排序。
    return r1 == 0 ? r2 : r1;
};

Array.sort 不接受比较器,所以你需要将数组转换为列表并在其上进行排序。这也会改变数组,因为对象数组会备份从 Arrays.asList 返回的列表。

Collections.sort(Arrays.asList(var), comp);
System.out.println(Arrays.toString(var));

打印结果:

[a2, a3, a11, b1, b2, b3, c2, c3, c13]
英文:

Here's a relatively simple approach. However, for something like this I would create a class to hold the alpha and numeric parts separately and write a comparator. When I print the objects I would simply have toString() return the original value.

This is basic. It does not check for malformed strings.

String[] var = {&quot;a3&quot;, &quot;a2&quot;, &quot;a11&quot;, &quot;b1&quot;, &quot;b2&quot;, &quot;b3&quot;, &quot;c3&quot;,  &quot;c13&quot;, &quot;c2&quot;};  

Comparator&lt;String&gt; comp = (a,b)-&gt;{
      // split the strings between the last char and the first digit 
	  String[] v1 = a.split(&quot;(?&lt;!\\d)(?=\\d)&quot;);
	  String[] v2 = b.split(&quot;(?&lt;!\\d)(?=\\d)&quot;);
      
      // convert each integer part to an int.
	  int n1 = Integer.valueOf(v1[1]);
	  int n2 = Integer.valueOf(v2[1]);

      // compare each and get the result
	  int r1 = v1[0].compareTo(v2[0]);
	  int r2 = Integer.compare(n1,n2);

      // first sort on r1 (the strings).  If they are equal ( r1 == 0) 
      // then sort on the result of comparing the integers.
	  return r1 == 0 ? r2 : r1;
};

Array.sort does not take a comparator so you need to convert the array to a list and sort on that. This will also change the array since object arrays back up the returned list from Arrays.asList.

	  
Collections.sort(Arrays.asList(var),comp);
System.out.println(Arrays.toString(var)); 

Prints

[a2, a3, a11, b1, b2, b3, c2, c3, c13]


</details>



huangapple
  • 本文由 发表于 2020年9月18日 21:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63957125.html
匿名

发表评论

匿名网友

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

确定