如何首先按字母开头,然后按数字对字符串数组进行排序

时间:2016-05-19 22:34:37

标签: android arrays sorting

我正在尝试对大型String数组进行排序。我想根据我的订单,主要按照开头的单词进行排序。但是我有多个具有相同开头词的项目,所以我希望能够在最初的单词后面输入数字。

例如:

String[] unordered = {"Pencil 2", "Cat 3", "Pencil 1", "Cat 1", "Cat 2"}

我希望sort的结果是:

String[] ordered = {"Cat 1", "Cat 2", "Cat 3", "Pencil 1", "Pencil 2"}

现在看来,我正在整理我的清单,而忽略了之后的数字。所以我最终得到这样的东西:

String somewhat ordered = {"Cat 3", "Cat 1", "Cat 2", "Pencil 2", "Pencil 1"}

我的代码现在看起来像这样:

String[] sortlist = {"Pencil 2", "Cat 3", "Pencil 1", "Cat 1", "Cat 2"}

String[] Bookorder = {"Cat", "Pencil", etc...}

List<String>[] results = new List[bookorder.length];
            LinkedList<String> remainders = new LinkedList<String>();

            for (String word : sortlist) {
                boolean found = false;
                for (int i = 0; i < bookorder.length; i++) {
                    if (word.startsWith(bookorder[i])) {
                        if (results[i] == null) results[i] = new LinkedList<String>();
                        results[i].add(word);
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    remainders.add(word);
                }
            }

            finalsortedlist = new ArrayList<String>(sortlist.length);
            for (List<String> result : results) {
                if (result != null) {
                    finalsortedlist.addAll(result);
                }
            }

            finalsortedlist.addAll(remainders);

            String[] finalsortedarray = new String[finalsortedlist.size()];
            finalsortedarray = finalsortedlist.toArray(finalsortedarray);

            dblinesequivalent = Arrays.toString(finalsortedarray);

除了不对数字进行排序这一事实外,这项工作很有效。我无法弄清楚如何做到这一点,而不会弄乱之前的单词排序。

我很感激您愿意提供的任何帮助。提前谢谢。

编辑:

我不希望按字母顺序对项目进行排序,它们会根据指定所需顺序的其他列表进行排序。

1 个答案:

答案 0 :(得分:1)

private void reorder() {
    String[] unordered = {"Pencil 2", "Cat 3", "Pencil 1", "Cat 1", "Cat 2"};
    Arrays.sort(unordered);
}

编辑:这是一个适用于所有数字的版本

private static void reorder() {
    String[] unordered = { "Pencil 2", "Cat 11", "Pencil 1", "Cat 1", "Cat 2" };
    Arrays.sort(unordered);
    System.out.println("After Arrays.sort()");
    for (String s : unordered)
        System.out.println(s);
    for (int i = 0; i < unordered.length; i++) {
        for (int j = i + 1; j < unordered.length - 1; j++) {
            if (unordered[i].substring(0, unordered[i].indexOf(' '))
                    .equals(unordered[j].substring(0, unordered[j].indexOf(' ')))) {
                String numI = unordered[i].substring(unordered[i].indexOf(' ') + 1);
                String numJ = unordered[j].substring(unordered[j].indexOf(' ') + 1);
                if (Integer.parseInt(numI) > Integer.parseInt(numJ)) {
                    String tmp = unordered[i];
                    unordered[i] = unordered[j];
                    unordered[j] = tmp;
                }
            }
        }
    }
    System.out.println("After sorting");
    for (String s : unordered)
        System.out.println(s);
}