排序字母数字字符串java

时间:2013-01-15 12:24:15

标签: java sorting

我有这个数组存储用户正在添加的一些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中对字母数字字符串进行排序。

4 个答案:

答案 0 :(得分:3)

我决定使用@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.
            }
         }
    }
    

答案 1 :(得分:2)

在创建Collections.sort()之后,只需使用ArrayList方法:

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]

答案 2 :(得分:1)

创建自定义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()并保持您现在发布的代码与现在完全相同。

答案 3 :(得分:1)

我认为你所问的与此类似:

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

您可以将字符串分解为纯字符串和数字字符串。 例如:abc123将分成&#34; abc&#34;和&#34; 123&#34; 您可以将字母字符串与正常比较进行比较,然后对&#34; 123&#34;进行排序。这种字符串,你有两个选择: 1:将其转换为整数然后进行比较 2:如果数字不适合整数范围,您可以逐字母进行比较。

对于例如&#34; 123&#34; vs&#34; 133&#34; 比较&#34; 1&#34;和&#34; 1&#34; =相等 比较&#34; 2&#34;和&#34; 3&#34; =更大&#34; 123&#34; &LT; &#34; 133&#34 ;.

选项2更准确,防错更少。

相关问题