Java比较具有字符和数字的字符串

时间:2012-02-17 20:59:54

标签: java string compare

在Java中,我想比较和排序包含字符和数字的字符串。例如:

应分类A15,D35,A17,C45,B27,C30

A15 A17 B27 C30 C45 D35。我不确定如何比较其中两个元素,因为它们包含一个字符串和一个数字。有人可以帮帮我吗?

4 个答案:

答案 0 :(得分:6)

如果您的数字总是两位数,那么只需将整个事物作为字符串进行比较。十进制数也是字符串。

如果您需要对A9,A54和A123456进行排序并希望数字按其数值进行排序,那么这是另一回事。在这种情况下,您可能需要编写自己的比较函数,将字符串拆分为其组成部分。

答案 1 :(得分:3)

所述比较功能的一个例子:

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.google.common.collect.Lists;

class Scratch {

    public static void main(String[] args) {
        final List<String> in = Lists.newArrayList("D35", "A1", "C7", "A25", "A131");

        Collections.sort(in, new Comparator<String>() {
            @Override
            public int compare(String left, String right) {
                int letter = Character.compare(left.charAt(0), right.charAt(0));
                if (0 != letter)
                    return letter;

                return Long.compare(Long.parseLong(left.substring(1)), Long.parseLong(right.substring(1)));
            }
        });

        System.out.println(in);
    }
}

答案 2 :(得分:0)

public class StringComparator implements Comparator<String>{

    String string_char[]= {"a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","q","w","e","r","t","y","u","i","o","p"};
    public enum DataType{
        STRING,INTEGER,SPECIAL_CHAR;
    }

    public Integer getDataTypePriority(DataType type){
        try{
            switch (type) {
                case SPECIAL_CHAR:
                    return 1;
                case INTEGER:
                    return 2;
                case STRING:
                    return 3;
                default:
                    break;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public boolean isStringType(String val){
        for(String temp: string_char)
            if(temp.equalsIgnoreCase(val))
                return true;
        return false;   
    }
    public DataType getDataType(Character val){
        DataType type = null;
        try{
            Integer.parseInt(val.toString());
            type = DataType.INTEGER;
        }catch(Exception e){
            if(isStringType(val.toString()))
                type = DataType.STRING;
            else
                type = DataType.SPECIAL_CHAR;
        }
        return type;
    }
    @Override
    public int compare(String new_val, String old_val) {
        int result = 0;
        try{
            if(new_val == null)
                return -1;
            else if(old_val == null)
                return 1;
            StringBuilder old_Int = new StringBuilder();
            StringBuilder new_Int = new StringBuilder();
            DataType oldDataType = null;
            DataType newDataType = null;

            int old_length = old_val.length();
            int new_length = new_val.length();
            int max = old_length;
            if(new_length < old_length)//equals
                max = new_val.length();

            StringBuilder old_Char = new StringBuilder();
            StringBuilder new_Char = new StringBuilder();
            for(int i=0;i<max;i++){
                old_Char.setLength(0);
                new_Char.setLength(0);
                old_Char.append(old_val.charAt(i));
                new_Char.append(new_val.charAt(i));
                oldDataType = getDataType(old_val.charAt(i));
                newDataType = getDataType(new_val.charAt(i));
                if(oldDataType != newDataType){
                    if(getDataTypePriority(newDataType) > getDataTypePriority(oldDataType))
                        return 1;
                    else
                        return -1;
                }else{//same
                    if(newDataType.equals(DataType.STRING)){
                        if(old_Int.length() >0){//clearing string builder
                            old_Int.setLength(0);
                            new_Int.setLength(0);
                        }
                        if(new_Char.toString().compareTo(old_Char.toString()) >0)
                            return 1;
                        else if(new_Char.toString().compareTo(old_Char.toString()) < 0)
                            return -1;                              
                    }else if(newDataType.equals(DataType.INTEGER)){
                        old_Int.append(old_val.charAt(i));
                        new_Int.append(new_val.charAt(i));
                        while(i+1<max){
                            i+=1;
                            oldDataType = getDataType(old_val.charAt(i));
                            newDataType = getDataType(new_val.charAt(i));
                            if(oldDataType.equals(DataType.INTEGER))
                                old_Int.append(old_val.charAt(i));
                            if(newDataType.equals(DataType.INTEGER))
                                new_Int.append(new_val.charAt(i));
                            if(oldDataType != newDataType)
                                break;
                        }
                        if(new_length > max){
                            while(i+1 < new_length){
                                i+=1;
                                newDataType = getDataType(new_val.charAt(i));
                                if(newDataType.equals(DataType.INTEGER))
                                    new_Int.append(new_val.charAt(i));
                                else 
                                    break;
                            }
                        }else if(old_length >max){
                            while(i+1<old_length){
                                i+=1;
                                oldDataType = getDataType(old_val.charAt(i));
                                if(oldDataType.equals(DataType.INTEGER))
                                    old_Int.append(old_val.charAt(i));
                                else 
                                    break;
                            }
                        }
                        Integer n = Integer.parseInt(new_Int.toString());
                        Integer o = Integer.parseInt(old_Int.toString());
                        if(n > o)
                            return 1;
                        else if(n < o) 
                            return -1;
                    }else{//special char
                        if(old_Int.length() >0){//clearing string builder
                            old_Int.setLength(0);
                            new_Int.setLength(0);
                        }
                        if(new_Char.toString().compareTo(old_Char.toString()) >0)
                            return 1;
                        else if(new_Char.toString().compareTo(old_Char.toString()) < 0)
                            return -1;              
                    }
                }
            }
            return new_val.compareTo(old_val);

        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

}

答案 3 :(得分:0)

基本上,如果你需要对字符串列表进行排序,你可以使用流:

     List<String> yourSortedListOfStrings = yourListOfStrings.stream()
        .sorted()
        .collect(Collectors.toList());