排序字母数字字符串java
发布时间:2020-12-15 08:49:38 所属栏目:Java 来源:网络整理
导读:我有这个数组存储用户添加的一些URL的后缀: [U2,U3,U1,U5,U8,U4,U7,U6] 当我这样做: for (MapString,String map : getUrlAttachments()) { String tmpId = map.get("id"); //it receives the U2,in the 1st iteration,then U3,then U1,... if (tmpId.charA
我有这个数组存储用户添加的一些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,U6,U8]那样排序的数据. 我希望你的帮助能够知道我能做到这一点的最佳方式. 我想创建一个列出id的数组然后排序,但我不知道如何在java中对字母数字字符串进行排序. 解决方法
我决定使用@Abu给出的想法,但我改编了它:
>我检查用户试图添加的网址的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. } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |