2013-10-01 79 views
5

我有大寫字母和數字組成的字符串,我必須「邏輯」排序,並存儲在數據庫中的字段。我已經將更新/更改/查詢部分納入了數據庫。我很難在邏輯上對這個字符串進行排序。邏輯排序的大寫字母和數字的混合串

這裏去,我希望我能很好地解釋這一點。

鑑於這一組字符串 AB1 AB2 AB3 A11 AB10

我需要這些阿爾法有點像這樣

A11 AB1 AB2 AB3 AB10

爲了爲了實現這一點,我相信我需要爆炸字符串。因爲目前正試圖字母排序產生A11 AB1 AB10 AB2 AB3

編輯:我需要能夠存儲的分解字符串和非爆炸串能夠與其它程序進行排序。

這裏是我認爲他們需要被分解並存儲在順序排序阿爾法

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; 
    }     
} 
+0

使用類似於基數排序的數字,其數字優先於字母。 – arynaq

+0

我會複製下面我做的評論如果我可以使用java對它們進行排序,這些工作對排序它們非常有用。問題是不同的程序需要對它們進行排序並與它們兼容,我需要'爆炸'字符串並使用爆炸字符串進行排序並顯示常規值。我知道不是'正常'。在數據庫中會有兩個字段,其中一個將被稱爲section和一個名爲sort_section的字段。合理? – nkuebelbeck

回答

1

這句話「不知道如何像‘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。

+0

我不好意思添加最後一句話。我只是想到了同樣的事情,並回來更新我的代碼。 – nkuebelbeck

0

我會用空格完成這些字符串5個符號後,將使Radix Sort。我們可以將所有符號作爲字符進行比較。

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]); 
1

這是我如何用我的基數排序的想法對其進行排序:

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; 
} 

隨着輸入

String[] strings = new String[] { "AB1", "AB2", "AB3", "A11", "AB10" }; 
    System.out.println(Arrays.toString(radixSort(strings))); 

和輸出

[A11, AB1, AB2, AB3, AB10] 

我不知道這是最有效的方法,但它完成了工作。

+0

這些工作偉大的排序他們,如果我可以使用java排序他們。問題是不同的程序需要對它們進行排序並與它們兼容,我需要'爆炸'它們並使用分解的字符串進行排序並顯示常規值。我知道不是'正常'。在數據庫中會有兩個字段,其中一個將被稱爲section和一個名爲sort_section的字段。合理? – nkuebelbeck

+0

沒有意識到第二個要求,將它們包裝在另一個對象中,比如marco建議這樣做,那麼添加getter或使用正則表達式來分割字符串,我將它們分類爲字母和數字值。 – arynaq

+0

這是我的不好,我添加到原來的文章 – nkuebelbeck

1

,你可以使用另一個類作爲您的字符串中的特殊表現。是這樣的:

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 : ""); 
    } 
} 

可以把您的當前字符串到這個類,排序並將其轉換回爲需要

0

這是我的代碼。我相信它可以精簡,這是我有一個大腦孩子,需要寫作的那些停電時刻之一。這是行不通的,如果這串數字是超過500個字符長...

更新:不醜

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; 
0

你真的需要將數據之前把它在數據庫排序?考慮讓數據庫爲您完成工作。

假設您將值直接寫入數據庫。你的數據庫可能允許你做類似我的事情。在DB2中,爲了只獲取字母,我會將所有數字轉換爲空格,然後刪除所有空格。相同的概念可以適用於只獲取數字。

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),' ','')), 
      ..... 
      ) 

我的看法的時候做到這一點,這是簡單的邏輯,更少的代碼,更容易測試/調試,幫助減少缺陷的風險,並且是有人更容易保持。當然,您的里程可能會因您的數據庫而異。但是這種方法似乎值得考慮。

相關問題