Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对大写字母和数字的混合字符串进行逻辑排序_Java_Algorithm_Sorting - Fatal编程技术网

Java 对大写字母和数字的混合字符串进行逻辑排序

Java 对大写字母和数字的混合字符串进行逻辑排序,java,algorithm,sorting,Java,Algorithm,Sorting,我有一串大写字母和数字,必须“逻辑”排序并存储在数据库的字段中。我已经找到了数据库中的更新/更改/查询部分。我很难对这个字符串进行逻辑排序 好了,我希望我能解释清楚 给定这组字符串 AB1 AB2 AB3 A11 AB10 我需要这些像这样排列 A11 AB1 AB2 AB3 AB10 为了实现这一点,我相信我需要爆炸的字符串。因为目前尝试alpha排序会产生A11AB1AB10AB2AB3 编辑:我需要能够存储分解字符串和非分解字符串,以便能够与其他程序排序 以下是我认为需要如何分解和存储它们

我有一串大写字母和数字,必须“逻辑”排序并存储在数据库的字段中。我已经找到了数据库中的更新/更改/查询部分。我很难对这个字符串进行逻辑排序

好了,我希望我能解释清楚

给定这组字符串 AB1 AB2 AB3 A11 AB10

我需要这些像这样排列

A11 AB1 AB2 AB3 AB10

为了实现这一点,我相信我需要爆炸的字符串。因为目前尝试alpha排序会产生A11AB1AB10AB2AB3

编辑:我需要能够存储分解字符串和非分解字符串,以便能够与其他程序排序

以下是我认为需要如何分解和存储它们,以便对alpha进行排序

A11  -  A   11
AB1  -  AB   1
AB2  -  AB   2
AB3  -  AB   3
AB10 -  AB  10
有一些常数。该字符串将不大于5个位置。它将只包含大写字母和数字

这是我用我的代码得到的。作家们阻止了我,所以我希望能得到一些帮助。我想我需要找到它是否以一个字母开头,然后找到所有连续的字母,将它们向左移动,然后开始处理数字,找到所有连续的数字,并将它们向右对齐。也不确定像“A1B1”这样的东西会如何工作

for(int ii = 0;ii < sectionString.length() && ii< SECTIONSPACES;ii++){
               System.out.print("    Was previous a number? " + isPreviousANumber +         "\n");
try{
    String tmpString = sectionString.substring(ii,ii + 1 );
    int positionInCharArray = Integer.parseInt(tmpString);
    System.out.printf("    Position " + ii + " is number " + positionInCharArray + "\n");
    isPreviousANumber = true;        
}catch(Exception e){
    System.out.printf("    Position " + ii + " number is not a number " +      sectionString.substring(ii,ii) + "\n");
    isPreviousANumber = false;
    }                   
}
for(int ii=0;ii
我会在这些字符串中加上5个符号的空格,然后再加上。我们可以将所有符号作为字符进行比较

    String[] array = {"A11", "AB1", "AB2", "AB3", "AB10"};

    int i, j, length;
    for (i = 0; i < array.length; i++) {
        length = array[i].length();
        for (j = length; j < 5; j++) {
            array[i] += " ";
        }
    }

    Arrays.sort(array);

    for (int k = 0; k<array.length; k++)
        System.out.println(array[k]);
String[]数组={“A11”、“AB1”、“AB2”、“AB3”、“AB10”};
int i,j,长度;
对于(i=0;i对于(int k=0;k,以下是我如何使用基数排序思想对其进行排序:

public static String[] radixSort(String[] strings){
    // Pad the strings
    for(int i=0; i<strings.length; i++){
        strings[i] = String.format("%-5s", strings[i]);
    }

    // Radix sort them
    for (int digit = 0; digit < 5; digit++) {
        final int i = digit;
        Arrays.sort(strings, new Comparator<String>() {


            @Override
            public int compare(String o1, String o2) {
                return o1.charAt(i) - o2.charAt(i);
            }
        });
    }

    // Then trim the whitespaces we used to pad

    for (int i = 0; i < strings.length; i++) {
        strings[i] = strings[i].trim();
    }

    return strings;
}
和输出

[A11, AB1, AB2, AB3, AB10]

我不确定这是最有效的方法,但它完成了任务。

您可以使用另一个类作为字符串的特殊表示形式。类似如下:

public class AlphaNumericString implements Comparable<AlphaNumericString> {
    public final String alphaPart;
    public final Long numericPart;

    public AlphaNumericString(String string) {
        int index = 0;
        while (index < string.length() && !Character.isDigit(string.charAt(index))) {
            index++;
        }

        alphaPart = string.substring(0, index);

        if (index < string.length()) {
            numericPart = new Long(string.substring(index));
        } else {
            numericPart = null;
        }
    }

    @Override
    public int compareTo(AlphaNumericString other) {
        int stringCompareResult = alphaPart != null ? alphaPart.compareTo(other.alphaPart) : -1;

        if (stringCompareResult == 0) {
            return numericPart != null ? numericPart.compareTo(other.numericPart) : -1;
        } else {
            return stringCompareResult;
        }
    }

    @Override
    public String toString() {
        return (alphaPart != null ? alphaPart : "") + (numericPart != null ? numericPart : "");
    }
}
公共类AlphaNumericString实现可比较{
公共最终字符串字母部分;
公共最终长数字部分;
公共字母数字字符串(字符串){
int指数=0;
while(index
您可以将当前字符串转换为此类,根据需要对其进行排序并将其转换回类中

此备注“不确定类似'A1B1'的内容将如何工作…”在某种程度上增加了问题的复杂性。以下内容适用于所有情况

方法:

将字符串划分为标记。标记可以是字母,也可以是连续的数字。将每个数字标记用前导空格填充到五个字符中。将标记连接起来形成分解的字符串

从5个字符的原始字符串中,最长的分解字符串将为17个字符

生成的分解字符串可以按任何程序或SQL“ORDERED by”子句进行排序

示例:

1A1A1   "    1A    1A    1"
11A11   "   11A   11"
1111A   " 1111A"
11111   "11111"
A1      "A    1"
A1B1    "A    1B    1"
A1C     "A    1C"
A2      "A    2"
A2B1    "A    2B    1"
A10     "A   10"
A10B1   "A   10B    1"
A11     "A   11"
AA1     "AA    1"
AB1     "AB    1"
AB2     "AB    2"
AB10    "AB   10"
ABC     "ABC"
伪代码:

// original = "section" string
exploded = ""
prevdigits = false
for ii from 1 to length(original) {
   ch = original[ii]
   if (ch is a digit) then {
      if not prevdigits then {
         token = ""
         prevdigits = true
      }
      token = token+ch
   } else { // letter
      if prevdigits then {
         exploded = exploded + spaces(5-length(token)) + token
         prevdigits = false
      }
      exploded = exploded + ch
   }
}

-Al.

这是我的代码。我相信它可以简化,那是我大脑发育不好需要写作的时候之一。如果数字字符串长度超过5个字符,这就不起作用了

更新:不那么难看

private String buildPieceSortNumber(String pieceNumber){
    final int INTSPACES = 5;
    final String SPACE = " ";
    String explodedSection = "";        
    char[] charArray = pieceNumber.toCharArray();
    String ints = "";
    for(int i = 0;i < charArray.length;i++){
        if(Character.isDigit(charArray[i])){
            //add to the int string
            ints += charArray[i];
            //check if the next character in the array is a number
            int nextChar = i + 1;
            //make sure we don't go past the end of the string                
            if(nextChar < charArray.length){
                if(!Character.isDigit(charArray[nextChar])){
                    //end of numbers, take ints string, and add padding up to five positions
                    while(ints.length() < INTSPACES){
                        ints = SPACE + ints;
                    }
                    //add the int string to the end of the exploded string
                    explodedSection += ints;                        
                    //clear the int string 
                    ints = "";
                    }
            }else{
                //end of numbers, take ints string, and add padding up to five positions
                while(ints.length() < INTSPACES){
                    ints = SPACE + ints;
                }
                //add the int string to the end of the exploded string
                explodedSection += ints;
                //clear the int string 
                ints = "";
            }                
        }else{
            explodedSection += charArray[i];                                                            
        }
    }
    return explodedSection;
private String buildPieceSortNumber(字符串片段编号){
最终整数空间=5;
最终字符串空格=”;
字符串explodedSection=“”;
char[]charArray=pieceNumber.toCharArray();
字符串ints=“”;
for(int i=0;iprivate String buildPieceSortNumber(String pieceNumber){
    final int INTSPACES = 5;
    final String SPACE = " ";
    String explodedSection = "";        
    char[] charArray = pieceNumber.toCharArray();
    String ints = "";
    for(int i = 0;i < charArray.length;i++){
        if(Character.isDigit(charArray[i])){
            //add to the int string
            ints += charArray[i];
            //check if the next character in the array is a number
            int nextChar = i + 1;
            //make sure we don't go past the end of the string                
            if(nextChar < charArray.length){
                if(!Character.isDigit(charArray[nextChar])){
                    //end of numbers, take ints string, and add padding up to five positions
                    while(ints.length() < INTSPACES){
                        ints = SPACE + ints;
                    }
                    //add the int string to the end of the exploded string
                    explodedSection += ints;                        
                    //clear the int string 
                    ints = "";
                    }
            }else{
                //end of numbers, take ints string, and add padding up to five positions
                while(ints.length() < INTSPACES){
                    ints = SPACE + ints;
                }
                //add the int string to the end of the exploded string
                explodedSection += ints;
                //clear the int string 
                ints = "";
            }                
        }else{
            explodedSection += charArray[i];                                                            
        }
    }
    return explodedSection;
SELECT replace(translate(inp, @spaces, @digits),' ','') as alpha, 
       int(replace(translate(inp, @spaces, @letters),' ','')) as nbr,
       ....
INSERT INTO yourtable ( item, alpha, nbr, ..... )
     VALUES (inp,
             replace(translate(inp, @spaces, @digits),' ',''),
             int(replace(translate(inp, @spaces, @letters),' ','')),
             .....
            )