4

我有这个数组存储用户添加的一些 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

4 回答 4

3

只需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]
于 2013-01-15T12:28:44.323 回答
3

我决定使用@Abu 给出的想法,但我对其进行了调整:

  1. 我检查用户尝试添加的 url 的 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.
            }
         }
    }
    
于 2013-01-17T15:32:50.010 回答
2

我认为您的要求与此类似:

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

您可以将字符串分解为纯字符串和数字字符串。例如: abc123 将被拆分为“abc”和“123” 您可以将字母字符串与普通比较进行比较,然后对“123”这样的字符串进行排序,您有两个选择:1:将其转换为整数,然后比较 2 :如果数字不在整数范围内,您可以逐个字母进行比较。

例如“123”与“133”比较“1”和“1”=相等比较“2”和“3”=更大所以“123”<“133”。

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

于 2015-03-17T23:45:35.433 回答
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()并保持您发布的代码与现在完全相同。

于 2013-01-15T12:29:22.423 回答