字母和数字排序 java,排序字母数字字符串java

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

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

When I do this:

for (Map 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);

}

My problem:

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.

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.

解决方案

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

I check the ids of the urls the user is trying to add,

I remove the alphabetic suffix in this id and then I create an ArrayList to store the numerical part of each id.

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 urlSorted = new ArrayList();

//sort the url ids

for (Map 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 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.

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值