Java排序列表< SqlRow>按日期作为字符串 [英] Java sort List <SqlRow> by date as String

查看:116
本文介绍了Java排序列表< SqlRow>按日期作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型的列表,我想排序这个列表的日期元素。我googled,我看到一些可比较的解决方案,但是有没有可能这样做,而不实现我的课程中的界面?
我的列表如下所示:
list [{id = 33,text = test1,date = 06.02.15},{id = 81,text = test2,date = 10.01.15} ]
现在我想比较两个元素,并希望按日期排序。但日期也是在String中。
你有什么想法吗?

I have a list from the Type and I would like to sort this list by an element of date. I googled and I saw some solutions with comparable, but is there a possibility to do this without implementing the interface in my class? My list looks like as in the following: list[{id=33, text=test1, date=06.02.15}, {id=81, text=test2, date=10.01.15}] Now I would like to compare both elements and would like to order them descending by date. But date is also in String. Do you have any ideas?

提前非常感谢。

推荐答案

因此,如果日期存储在varchar2中,首先将它们转换为实际的Date对象,

So if date is stored in varchar2 than firstly Convert them to an actual Date object,

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd h:m");
System.out.println(sdf.parse(dateFromDataBase));

然后对日期对象进行比较对象

and then do the comparing on date Objects

if(date1.compareTo(date2)>0){
// Do whatever you want to do 
}

现在来排序列表< SqlRow>

您可以使用 @Srinivasu Talluri
中提到的比较器接口,但如果您不感兴趣使用这些界面,使您的生活变得简单,您可以使用泡泡排序插入排序,并根据上述日期进行比较

you can use comparator Interface as mentioned by @Srinivasu Talluri but if you are not interested in using these interfaces which literally make you life easy than you can use either bubble sort or Insertion sort and compare on the basis of dates as mentioned above

这篇关于Java排序列表&lt; SqlRow&gt;按日期作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆