java 排序字母数字字符串java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14337682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting alphanumeric strings java
提问by periback2
I have this array storing the suffix of some URLs the user is adding:
我有这个数组存储用户添加的一些 URL 的后缀:
[U2, U3, U1, U5, U8, U4, U7, U6]
When I do this:
当我这样做时:
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);
}
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]
.
我想For
通过传递另一个映射排序的数据的列表来更改此条件[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.
我想过创建一个列出 id 的数组,然后进行排序,但我不知道如何在 java 中对字母数字字符串进行排序。
采纳答案by periback2
I decide to use the idea @Abu gave, but I adapted it:
我决定使用@Abu 给出的想法,但我对其进行了修改:
- 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 sortedArrayList
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. } } }
- 我检查了用户尝试添加的 url 的 id,
- 我删除了这个 id 中的字母后缀,然后我创建了一个
ArrayList
来存储每个 id 的数字部分。 我
ArrayList
像@Abu 在他的回答中教我的那样排序,然后我验证每个 id 按照ArrayList
它应该添加的顺序排序。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. } } }
回答by Abubakkar
Just use Collections.sort()
method after creating an ArrayList
of your values like this:
只需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);
Output :
输出 :
Before : [U2, U1, U5, U4, U3]
After : [U1, U2, U3, U4, U5]
回答by Gaurav
I think what you are asking is similar to this one :
我认为你问的是类似于这个:
http://www.davekoelle.com/alphanum.html
http://www.davekoelle.com/alphanum.html
You can break string into pure string and numeric string. for e.g: abc123 would be split into "abc" and "123" You can compare alphabetic string with normal comparison and then to sort "123" such kind of strings, you have two options: 1: Convert it into Integer and then compare 2: If number does not fit in Integer range, you can compare letter by letter.
您可以将字符串分解为纯字符串和数字字符串。例如:abc123 将被拆分为“abc”和“123” 您可以将字母字符串与正常比较进行比较,然后对“123”这样的字符串进行排序,您有两种选择: 1:将其转换为整数,然后比较 2 : 如果数字不适合整数范围,您可以逐个字母比较。
for eg "123" vs "133" compare "1" and "1" = equal Compare "2" and "3" = greater so "123" < "133".
例如“123”与“133”比较“1”和“1”=相等比较“2”和“3”=更大所以“123”<“133”。
Option 2 is more accurate and less error proof.
选项 2 更准确,错误证明更少。
回答by Marko Topolnik
Create a custom Comparator<Map<String,String>>
:
创建自定义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));
}
}
and then use Arrays.sort(urlAttachments, new IdComparator());
prior to iterating over it. Depending on details, you may push this sorting logic into getUrlAttachments()
and keep the code you have posted exactly as it is now.
然后Arrays.sort(urlAttachments, new IdComparator());
在迭代之前使用。根据详细信息,您可以将此排序逻辑推入getUrlAttachments()
并保持您发布的代码与现在完全一样。