如何解决用自定义格式的日期对字符串数组进行排序
|| 我有一个像String str[]={\"jan2011\",\"feb2011\",\"jan2010,\"mar2012\"};
如何按日期排序:jan2010,jan2011,feb2011,mar2012
谢谢你的进步(不要再使用字符串函数了)
解决方法
i
String[] unsorted={\"jan2011\",\"feb2011\",\"jan2010\",\"mar2012\"};
List<String> unsortedList = Arrays.asList(unsorted);
Collections.sort(unsortedList);
String[] sorted = (String[])unsortedList.toArray();
这应该做...
, 假设您可以使用版本6,则可以使用内置的
Collections.insertionSort(datavector,comparator);
然而:
您需要将字符串移动到Vector中
您需要提供Comparator类以满足您的特定需求
Comparator类可能类似于以下内容:
public class DatestringComparator implements Comparator{
public int compare(String s1,String s2) {
// create Date instance based on the content of the input string
Date d1 = ....;
Date d2 = ....;
return d1.compareTo(d2);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。