English 中文(简体)
• 如何分类Aplha数字插图?
原标题:how to sort Aplha numeric strings?
  • 时间:2011-10-05 13:52:18
  •  标签:
  • java
  • sorting

I have a list of String characters, They are mix characters that includes special characters too. I want to know how to sort them ascendingly ?

updated example aplha1.jpg aplha10.jpg 2.jpg 3.jpg 4.jpg aplha5.jpg

最佳回答

我希望这将为你工作。

public class AlphanumericSorting implements Comparator<String> {

    public int compare(String firstObjToCompare, String secondObjToCompare) {
        String firstString = firstObjToCompare.toString();
        String secondString = secondObjToCompare.toString();

        if (secondString == null || firstString == null) {
            return 0;
        }

        int lengthFirstStr = firstString.length();
        int lengthSecondStr = secondString.length();

        int index1 = 0;
        int index2 = 0;

        while (index1 < lengthFirstStr && index2 < lengthSecondStr) {
            char ch1 = firstString.charAt(index1);
            char ch2 = secondString.charAt(index2);

            char[] space1 = new char[lengthFirstStr];
            char[] space2 = new char[lengthSecondStr];

            int loc1 = 0;
            int loc2 = 0;

            do {
                space1[loc1++] = ch1;
                index1++;

                if (index1 < lengthFirstStr) {
                    ch1 = firstString.charAt(index1);
                } else {
                    break;
                }
            } while (Character.isDigit(ch1) == Character.isDigit(space1[0]));

            do {
                space2[loc2++] = ch2;
                index2++;

                if (index2 < lengthSecondStr) {
                    ch2 = secondString.charAt(index2);
                } else {
                    break;
                }
            } while (Character.isDigit(ch2) == Character.isDigit(space2[0]));

            String str1 = new String(space1);
            String str2 = new String(space2);

            int result;

            if (Character.isDigit(space1[0]) && Character.isDigit(space2[0])) {
                Integer firstNumberToCompare = new Integer(Integer.parseInt(str1.trim()));
                Integer secondNumberToCompare = new Integer(Integer.parseInt(str2.trim()));
                result = firstNumberToCompare.compareTo(secondNumberToCompare);
            } else {
                result = str1.compareTo(str2);
            }

            if (result != 0) {
                return result;
            }
        }
        return lengthFirstStr - lengthSecondStr;
    }

}
问题回答

Tom Christiansen(www.tchrist)所暗示的是,在比较案文时,你通常应使用一个讨论会网站java.text.Collator,其中考虑到真正的字母顺序(可能包括当地特定规则),而不仅仅是Unicode-unit lexographic Order。

为了进行分类,您将把每一条<代码>String改为。 关键,然后分类<代码> CollationKeys. 这样做是为了提高效率,你也可以直接利用讨论会召集人,但这会更糟。

努力执行 比较。

您 进入ArrayList或阵列,然后打电话.sort(Arrays.sort(

只有当有特殊需求时,才需要使用<条码>Comparator。 只是不推出自己的“qui”。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签