字符串排序

时间:2014-08-14 21:28:01

标签: java sorting

我有一个像

这样的字符串数组
{"Bart4", "Bart5", "Bart6", "Bart7", "Bart8", "Bart9", "Bart10",
 "Lisa", "Marge", "Homer", "Bart", "Bart1", "Bart2", "Bart3", "Bart11", "Bart12"}

我需要按此顺序对其进行排序

{"Bart", "Bart1", "Bart2", "Bart3", "Bart4", "Bart5", "Bart6",
 "Bart7", "Bart8", "Bart9", "Bart10", "Bart11", "Bart12", "Homer","Lisa", "Marge"}

如果我在java中使用标准排序函数我得到

 {"Bart", "Bart1", "Bart10", "Bart11", "Bart12", "Bart2", "Bart3",
  "Bart4", "Bart5", "Bart6", "Bart7", "Bart8", "Bart9","Homer","Lisa", "Marge"}

Java中是否有预定义的功能?

如果我想设计自定义排序,我该怎么做呢?

编辑:我在下面添加了大量编译错误的源代码

源代码 -

import java.util.*;

public class Main{
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
        @Override
        public int compareTo(String s1,String s2){
            int len1,len2,len,i,num1,num2;

            len1 = s1.length();
            len2 = s2.length();
            len = len1 > len2 ? len2 : len1;

            if(s1.equals(s2))
                return 0;

            for(i = 0; i < len; i++){
                if(isLetter(s1.charAt(i)) && isLetter(s2.charAt(i))){
                        if(s1.charAt(i) < s2.charAt(i))
                            return 1;
                        else if(s1.charAt(i) > s2.charAt(i))
                            return -1;
                }
                else if(isLetter(s1.charAt(i)) && isDigit(s2.charAt(i)))
                    return -1;

                else if(isDigit(s1.charAt(i)) && isLetter(s2.charAt(i)))
                    return 1;

                else if(isDigit(s1.charAt(i)) && isDigit(s2.charAt(i))){
                    num1 = Integer.parseInt(s1.substring(i,len1));
                    num2 = Integer.parseInt(s2.substring(i,len2));
                    return num1 > num2 ? -1 : 1;
                }       
            }
        }
    });

    public String newMember(String [] existingNames, String newName){
        int lv,arr_len,str_len,val,count,flag,setflag;

        count=-1;
        flag=1;
        setflag=0;
        arr_len = existingNames.length;
        str_len = newName.length();

        Arrays.sort(existingNames);
        print(existingNames,arr_len);

        for(lv = 0; lv < arr_len && flag == 1; lv++){
            if(existingNames[lv].startsWith(newName)){
                setflag = 1;
                count++;
                val = getValue(existingNames[lv],str_len);
                if(count < val && count == 0)
                    return newName;
                if(count < val && count != 0)
                    return newName.concat(count+"");
            }
            if(setflag == 1 && !existingNames[lv].startsWith(newName))
                flag = 0;
        }

        if(count == -1)
            return newName;

        return newName.concat(count + 1 + "");
    }

    public void print(String [] arr,int len){
        for(int i=0; i<len; i++)
            System.out.println(arr[i]);
    }

    public int getValue(String name,int len){
        int str_len = name.length();
        if(str_len == len)
            return 0;
        else
            return Integer.parseInt(name.substring(len,str_len));
    }

    public static void main(String [] args){
        String [] arr = {"Bart4", "Bart5", "Bart6", "Bart7", "Bart8", "Bart9", "Bart10","Lisa", "Marge", 
                        "Homer", "Bart", "Bart1", "Bart2", "Bart3","Bart11", "Bart12"};
        String name = "Bart";
        System.out.println(newMember(arr,name));
    }
}

编译错误:

compilation info
Main.java:4: error: <identifier> expected
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
                         ^
Main.java:4: error: <identifier> expected
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
                                     ^
Main.java:4: error: illegal start of type
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
                                       ^
Main.java:4: error: ')' expected
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
                                          ^
Main.java:4: error: ';' expected
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
                                                     ^
Main.java:4: error: <identifier> expected
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
                                                            ^
Main.java:4: error: illegal start of type
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
                                                             ^
Main.java:4: error: <identifier> expected
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
                                                              ^
Main.java:4: error: ';' expected
    java.util.Arrays.sort(stringArray ,new Comparator<String>(){
                                                               ^
Main.java:33: error: class, interface, or enum expected
    });
     ^
Main.java:35: error: class, interface, or enum expected
    public String newMember(String [] existingNames, String newName){
           ^
Main.java:38: error: class, interface, or enum expected
        count=-1;
        ^
Main.java:39: error: class, interface, or enum expected
        flag=1;
        ^
Main.java:40: error: class, interface, or enum expected
        setflag=0;
        ^
Main.java:41: error: class, interface, or enum expected
        arr_len = existingNames.length;
        ^
Main.java:42: error: class, interface, or enum expected
        str_len = newName.length();
        ^
Main.java:44: error: class, interface, or enum expected
        Arrays.sort(existingNames);
        ^
Main.java:45: error: class, interface, or enum expected
        print(existingNames,arr_len);
        ^
Main.java:47: error: class, interface, or enum expected
        for(lv = 0; lv < arr_len && flag == 1; lv++){
        ^
Main.java:47: error: class, interface, or enum expected
        for(lv = 0; lv < arr_len && flag == 1; lv++){
                    ^
Main.java:47: error: class, interface, or enum expected
        for(lv = 0; lv < arr_len && flag == 1; lv++){
                                               ^
Main.java:50: error: class, interface, or enum expected
                count++;
                ^
Main.java:51: error: class, interface, or enum expected
                val = getValue(existingNames[lv],str_len);
                ^
Main.java:52: error: class, interface, or enum expected
                if(count < val && count == 0)
                ^
Main.java:54: error: class, interface, or enum expected
                if(count < val && count != 0)
                ^
Main.java:56: error: class, interface, or enum expected
            }
            ^
Main.java:59: error: class, interface, or enum expected
        }
        ^
Main.java:64: error: class, interface, or enum expected
        return newName.concat(count + 1 + "");
        ^
Main.java:65: error: class, interface, or enum expected
    }
    ^
Main.java:67: error: class, interface, or enum expected
    public void print(String [] arr,int len){
           ^
Main.java:68: error: class, interface, or enum expected
        for(int i=0; i<len; i++)
                     ^
Main.java:68: error: class, interface, or enum expected
        for(int i=0; i<len; i++)
                            ^
Main.java:70: error: class, interface, or enum expected
    }
    ^
Main.java:72: error: class, interface, or enum expected
    public int getValue(String name,int len){
           ^
Main.java:74: error: class, interface, or enum expected
        if(str_len == len)
        ^
Main.java:76: error: class, interface, or enum expected
        else
        ^
Main.java:78: error: class, interface, or enum expected
    }
    ^
Main.java:80: error: class, interface, or enum expected
    public static void main(String [] args){
                  ^
Main.java:83: error: class, interface, or enum expected
        String name = "Bart";
        ^
Main.java:84: error: class, interface, or enum expected
        System.out.println(newMember(arr,name));
        ^
Main.java:85: error: class, interface, or enum expected
    }
    ^
41 errors
  stdout

如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

对于自定义排序,您可以使用Collection并实施Comparator。例如:

String [] a = {"Bart4", "Bart5", "Bart6", "Bart7", "Bart8", 
    "Bart9", "Bart10", "Lisa", "Marge", "Homer", "Bart", 
    "Bart1", "Bart2", "Bart3", "Bart11", "Bart12"};

java.util.Arrays.sort(a, new Comparator<String>() {

    final String regex = "^([\\D]+)([0-9]+)$";
    Pattern p = Pattern.compile(regex);
    Matcher m1, m2;

    @Override
    public int compare(String o1, String o2) {
        m1 = p.matcher(o1);
        m2 = p.matcher(o2);

        if (m1.matches() && m2.matches()) {
            if (m1.group(1).equals(m2.group(1))) 
                return Integer.valueOf(m1.group(2)).compareTo(Integer.valueOf(m2.group(2)));
        } 

        return o1.compareTo(o2);
    }
});

for (String s : a) System.out.println(s);

输出:

Bart
Bart1
Bart2
Bart3
Bart4
Bart5
Bart6
Bart7
Bart8
Bart9
Bart10
Bart11
Bart12
Homer
Lisa
Marge

编辑:直接对数组进行排序,而不是使用Arrays.asList()

答案 1 :(得分:1)

您必须使用Comparator并定义自己的逻辑。Comparator将输入两个String

String [] stringArray = {"Bart4", "Bart5", "Bart6", "Bart7", "Bart8", "Bart9", "Bart10",
 "Lisa", "Marge", "Homer", "Bart", "Bart1", "Bart2", "Bart3", "Bart11", "Bart12"};

java.util.Arrays.sort(stringArray ,new Comparator<String>(){

            @Override
            public int compare(String s1, String  s2) {
                //You will need to build your logic here ..You will have to extract numbers //from the string and compare the substring and the number with other sub string and number
            }
});

答案 2 :(得分:0)

你能把Bart1换成Bart01吗? 如果可以的话,简单的字母排序就足够了。