簡體   English   中英

排序字母數字字符串java

[英]Sorting alphanumeric strings java

我有這個數組存儲用戶添加的一些URL的后綴:

[U2, U3, U1, U5, U8, U4, U7, U6]

當我這樣做:

for (Map<String, String> map : getUrlAttachments()) {
            String tmpId = map.get("id"); //it receives the U2, in the 1st iteration, then U3, then U1,...
            if (tmpId.charAt(0) == 'U') {
                tmpId.charAt(1);//2, then 3, then 1,...
                String url = map.get("url");
                String description = map.get("description");
                URLAttachment attachment;
                String cleanup = map.get("cleanup");
                if (cleanup == null && url != null && description != null) {
                    attachment = new URLAttachmentImpl();
                    attachment.setOwnerClass(FileUploadOwnerClass.Event.toString());
                    attachment.setUrl(url);
                    attachment.setDescription(description);
                    attachment.setOwnerId(auctionHeaderID);
                    attachment.setUrlAttachmentType(URLAttachmentTypeEnum.EVENT_ATTACHMENT);
                    attachment.setDateAdded(new Date());
                    urlBPO.save(attachment);

            }

我的問題:

我想通過傳遞另一個列表來改變這個For條件,這些列表映射了像[U1, U2, U3, U4, U5, U6, U7, U8]那樣排序的數據。

我希望你的幫助能夠知道我能做到這一點的最佳方式。

我想創建一個列出id的數組然后排序,但我不知道如何在java中對字母數字字符串進行排序。

在創建值的ArrayList之后,只需使用Collections.sort()方法,如下所示:

ArrayList<String> a = new ArrayList<String>();
a.add("U2");
a.add("U1");
a.add("U5");
a.add("U4");
a.add("U3");
System.out.println("Before : "+a);
Collections.sort(a);
System.out.println("After : "+a);

輸出:

Before : [U2, U1, U5, U4, U3]
After : [U1, U2, U3, U4, U5]

我決定使用@Abu給出的想法,但我改編了它:

  1. 我檢查用戶試圖添加的網址的ID,
  2. 我刪除了此id中的字母后綴,然后創建一個ArrayList來存儲每個id的數字部分。
  3. 我將這個ArrayList排序為像@Abu一樣在他的答案中教我,然后我按照它應該添加的順序驗證這個排序的ArrayList中的每個id。

     ArrayList <Integer> urlSorted = new ArrayList<Integer>(); //sort the url ids for (Map<String, String> map : getUrlAttachments()) { String tmpId = map.get("id"); if (tmpId.charAt(0) == 'U') { //gets the id, removing the prefix 'U' urlSorted.add( Integer.valueOf(tmpId.substring(1))); } } //sort the urlIds to check the sequence they must be added Collections.sort(urlSorted); //checks for each url id, compares if it's on the natural order of sorting to be added. for(Integer urlId: urlSorted) { for (Map<String, String> map : getUrlAttachments()) { String sortedId = "U"+urlId; String tmpId = map.get("id"); //compare the ids to add the 1, then 2, then 3... if (map.get("id").equals(sortedId)) { //code to save according to the sorted ids. } } } 

創建自定義Comparator<Map<String,String>>

public class IdComparator implements Comparator<Map<String,String>> {
  public int compare(Map<String,String> left, Map<String,String> right) {
    return orderKey(left).compareTo(orderKey(right));
  }
  static Integer orderKey(Map<String,String> m) { 
    return Integer.parseInt(m.get("id").substring(1)); 
  }
}

然后使用Arrays.sort(urlAttachments, new IdComparator()); 在迭代它之前。 根據細節,您可以將此排序邏輯推送到getUrlAttachments()並保持您已經發布的代碼與現在完全相同。

我想你所問的與這個類似:

http://www.davekoelle.com/alphanum.html

您可以將字符串分解為純字符串和數字字符串。 例如:abc123將分為“abc”和“123”您可以將字母字符串與正常比較進行比較,然后對“123”這類字符串進行排序,您有兩種選擇:1:將其轉換為整數然后比較2 :如果數字不適合整數范圍,您可以逐字母進行比較。

例如“123”vs“133”比較“1”和“1”=等於比較“2”和“3”=更大使得“123”<“133”。

選項2更准確,更少的錯誤證明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM