将列表的python排序转换为java代码。

将列表的python排序转换为java代码。,java,python,Java,Python,我正在尝试将这个将我的项目列表排序的python代码转换为java代码。如何在java中进行这种排序 python代码: 到目前为止,我对java的了解如下: //code something like this ArrayList<String> items = new ArrayList<String>(Arrays.asList("10H", "10S", "2H", "3S", "4S", "6C", "`7D", "8C", "8D", "8H", "11D",

我正在尝试将这个将我的项目列表排序的python代码转换为java代码。如何在java中进行这种排序

python代码:

到目前为止,我对java的了解如下:

//code something like this
ArrayList<String> items = new ArrayList<String>(Arrays.asList("10H", "10S", "2H", "3S", "4S", "6C", "`7D", "8C", "8D", "8H", "11D", "11H", "12H"));
Collections.sort(items)

谢谢

您需要使用自定义比较器来代替python脚本中声明的lambda表达式

Collections.sort(items,  new Comparator<String>() {
    private Pattern p = Pattern.compile("(\d+)[A-Z]*)");
    public int compare(String o1, String o2) {
        Matcher m1 = p.matcher(o1);
        Matcher m2 = p.matcher(o2);

        return Integer.valueOf(m1.group(0)).compareTo(Integer.valueOf(m2.group(0)));
    }
});

请注意,此比较器不比较字母组件,因为lambda表达式也不比较字母组件。

您需要使用自定义比较器来代替python脚本中声明的lambda表达式

Collections.sort(items,  new Comparator<String>() {
    private Pattern p = Pattern.compile("(\d+)[A-Z]*)");
    public int compare(String o1, String o2) {
        Matcher m1 = p.matcher(o1);
        Matcher m2 = p.matcher(o2);

        return Integer.valueOf(m1.group(0)).compareTo(Integer.valueOf(m2.group(0)));
    }
});

请注意,此比较器不比较字母组件,因为lambda表达式也不比较字母组件。

Collections.sort也接受比较器

所以,你可以这样做-

Collections.sort(items, new Comparator<String>() {
    @Override
    public int compareTo(String s1, String s2) {
           // do your magic here , by extracting the integer portion, comparing those
           // and then comparing the string to return 1 , 0 , -1
    }
});

Collections.sort也接受比较器

所以,你可以这样做-

Collections.sort(items, new Comparator<String>() {
    @Override
    public int compareTo(String s1, String s2) {
           // do your magic here , by extracting the integer portion, comparing those
           // and then comparing the string to return 1 , 0 , -1
    }
});
我在考虑收藏。sortT[]项目,。。但我不能让它工作。我对java真的很陌生我在考虑收藏。sortT[]项目,。。但我不能让它工作。我对java真的很陌生