输入是一个包含字符串格式的日期的列表。我有如下解决方案。但我觉得它可以变得更有效率。任何帮助将不胜感激。
//映射存放月份数据
HashMap<String,String> month = new HashMap<String,String>();
month.put("Jan","01");
month.put("Feb","02");
month.put("Mar","03");
month.put("Apr","04");
month.put("May","05");
month.put("Jun","06");
month.put("Jul","07");
month.put("Aug","08");
month.put("Sep","09");
month.put("Oct","10");
month.put("Nov","11");
month.put("Dec","12");
你可以将其视为输入
String[] input = {"20 Oct 2052",
"26 May 1960",
"06 Jun 1933",
"06 Jun 1933",
"06 Jun 1933",
};
ArrayList<Long> temp1 = new ArrayList<Long>();
比较结果
HashMap<Long,String> temp2 = new HashMap<Long,String>();
ArrayList<String> result = new ArrayList<String>();
for(int i = 0 ; i< input.length ; i++){
String j = "";
if(input[i].length() == 11){
j+= input[i].substring(7,11);
j+= month.get(input[i].substring(3,6));
j+=input[i].substring(0,2);
temp1.add(Long.parseLong(j));
temp2.put(Long.parseLong(j), input[i]);
}
}
排序结果
Collections.sort(temp1);
打印结果
System.out.println(temp1.toString());
最佳答案
Radix Sort是你的 friend 。只需使用此算法对字符串进行排序。这是最优解。
关于用于对格式为 dd MMM yyyy 的日期列表进行排序的 Java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52774434/