排序字母数字字符串java [英] Sorting alphanumeric strings java

查看:95
本文介绍了排序字母数字字符串java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组存储用户添加的一些URL的后缀:

I have this array storing the suffix of some URLs the user is adding:

[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]

I want to change this For condition by passing another list mapping the data sorted like [U1, U2, U3, U4, U5, U6, U7, U8].

我希望你的帮助知道什么是最好的方法我可以这样做。

I'd like your help to know what's the best way I could do this.

我想创建一个列出id的数组,然后排序,但我不知道如何在java中对字母数字字符串进行排序。

I thought about creating an array listing the ids and then sort then, but I don't know how exactly to sort alphanumeric strings in java.

推荐答案

我决定使用@Abu给出的想法,但我改编了它:

I decide to use the idea @Abu gave, but I adapted it:


  1. 我检查用户试图添加的网址的ID,

  2. 我删除了此ID中的字母后缀,然后创建了一个 ArrayList 用于存储每个id的数字部分。

  3. 我对此排序 ArrayList 喜欢@Abu在他的答案中教我,然后我按顺序验证这个排序的 ArrayList 中的每个id应该添加..

  1. I check the ids of the urls the user is trying to add,
  2. I remove the alphabetic suffix in this id and then I create an ArrayList to store the numerical part of each id.
  3. I sort this ArrayList like @Abu taught me in his answer and then I verify for each id in this sorted ArrayList in the sequence it should be added..

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


这篇关于排序字母数字字符串java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆