简体   繁体   English

使用字母前的字母对Java ArrayList进行排序

[英]Sort Java ArrayList with Letters before numbers

I am currently passing in an ArrayList to populate the titles of a TabLayout in Android. 我目前正在传递一个ArrayList来填充Android中TabLayout的标题。 The elements in the array are strings: ["RCP", "RYL", "1", "2", "3", "4", "5", "6"]. 数组中的元素是字符串: ["RCP", "RYL", "1", "2", "3", "4", "5", "6"]. Yet, not necessarily in that order. 然而,不一定按此顺序。

I would like the list to be returned with the Letter elements first (alphabetically) and then integers rising, incrementally. 我想首先使用Letter元素(按字母顺序)返回列表,然后逐步增加整数。

I have tried using the Collections.sort method, but this returns a list the rises numerically and then adds the "R" strings last: ["1", "2", "3", "4", "5", "6", "RCP", "RYL"] 我尝试过使用Collections.sort方法,但这会返回一个数字上升的列表,然后在最后添加“R”字符串: ["1", "2", "3", "4", "5", "6", "RCP", "RYL"]

To clarify, I would like to sort my ArrayList, so that it returns ["RCP", "RYL", "1", "2", "3", "4", "5", "6"] It also needs to be flexible, as the titles of the "R", strings, are likely to change. 为了澄清,我想对我的ArrayList进行排序,以便它返回["RCP", "RYL", "1", "2", "3", "4", "5", "6"]它也需要灵活,因为“R”,字符串的标题可能会改变。

Thanks 谢谢

There is an overload for Collections.sort which will take in a Comparator , which will allow you to define the sort order for your items. Collections.sort存在一个重载,它将接收一个Comparator ,它允许您定义项目的排序顺序。 In this case, all you have to do is define your own implementation of the Comparator interface which does as you please. 在这种情况下,您所要做的就是定义您自己的Comparator接口实现,并根据需要进行操作。

If you want to give priority to strings that represents numbers over strings that do not, you should consider writing your own Comparator and pass it to the Collections.sort method. 如果要优先考虑表示数字而非字符串的字符串,则应考虑编写自己的Comparator并将其传递给Collections.sort方法。

This is a simple approach to your problem: check if any of the strings is a number, if so, give priority to the string that is not a number. 这是解决问题的简单方法:检查任何字符串是否为数字,如果是,则优先考虑不是数字的字符串。 If both are numbers or strings, use string's compareTo method. 如果两者都是数字或字符串,请使用string的compareTo方法。

Hope it helps. 希望能帮助到你。

public class Main {

    private static int customCompare(String a, String b) {
        return isThereAnyNumber(a, b)
                ? isNumber(a) ? 1 : -1
                : a.compareTo(b);
    }

    private static boolean isThereAnyNumber(String a, String b) {
        return isNumber(a) || isNumber(b);
    }

    private static boolean isNumber(String s) {
        return s.matches("[-+]?\\d*\\.?\\d+");
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("RCP", "RYL", "1", "2", "3", "4", "5", "6");
        Collections.sort(list, Main::customCompare);
        System.out.println(list);
    }
}

UPDATE UPDATE

You can use an anonymous inner class that implements the Comparator interface in case you're not using Java 8. 如果您不使用Java 8,则可以使用实现Comparator接口的匿名内部类。

public class Main {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("RCP", "RYL", "1", "2", "3", "4", "5", "6");
        Collections.sort(list, new Comparator<String>() {
            private boolean isThereAnyNumber(String a, String b) {
                return isNumber(a) || isNumber(b);
            }

            private boolean isNumber(String s) {
                return s.matches("[-+]?\\d*\\.?\\d+");
            }

            @Override
            public int compare(String a, String b) {
                return isThereAnyNumber(a, b)
                        ? isNumber(a) ? 1 : -1
                        : a.compareTo(b);
            }
        });
        System.out.println(list);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM