我有一个97个元素的列表,我需要根据日期进行sot,其中包含格式为DD / MM / YYYY的日期,只有字符串类型,null对象和String类型的对象,即它有元素等如
我的日期字段中的数据示例:
12/02/2014
11/07/2017
null
India
现在我想根据日期字段对列表中的元素进行排序。
我想要的是所有带日期的条目应首先按升序排列,然后所有带有字符串的条目如india然后条目在null字段中为null,然后条目带有空日期字段,那么我应该如何在compareTo中对其进行编码方法
我已经看过很多例子,但没有得到理想的结果。 怎么做?请帮帮我?
ListAdapter.java
public class ListAdapter1 extends BaseAdapter {
static int c=0;
Context context;
List<ListViewRow> l=new ArrayList<>();
List<String> lv=new ArrayList<String>();
ListAdapter1(Context context, String[] getid,String name[], String[] email, String[] mobile, String[] pass, String[] date) {
this.context=context;
for(int i=0;i<getid.length;i++)
{
l.add(new ListViewRow(getid[i],name[i],email[i],mobile[i],pass[i],date[i]));
}
Collections.sort(l, new ListRowComparator());
}
@Override
public int getCount() {
return l.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_view_row, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.name);
TextView textView2 = (TextView) rowView.findViewById(R.id.email);
TextView textView3 = (TextView) rowView.findViewById(R.id.mobile);
TextView textView4 = (TextView) rowView.findViewById(R.id.pass);
TextView textView5 = (TextView) rowView.findViewById(R.id.date);
ListViewRow lvr=l.get(position);
textView1.setText(lvr.name);
textView2.setText(lvr.email);
textView3.setText(lvr.mobile);
textView4.setText(lvr.pass);
textView5.setText(lvr.date);
return rowView;
}
}
ListViewRow.java
public class ListViewRow {
String getid,name,email,mobile,pass,date;
ListViewRow( String getid, String name, String email, String mobile, String pass, String date)
{
this.getid = getid;
this.name = name;
this.email = email;
this.mobile = mobile;
this.pass = pass;
this.date= date;
}
}
ListRowComparator.java
public class ListRowComparator implements Comparator<ListViewRow> {
@Override
public int compare(ListViewRow o1, ListViewRow o2) {
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
int compareResult = 0;
try {
if (o1.date== null || o2.date== null)
{
return 0;
}
else {
compareResult = format.parse(o1.date).compareTo(format.parse(o2.date));
System.out.println("compare:" + compareResult);
}
} catch (ParseException e) {
e.printStackTrace();
}
return compareResult;
}
}
答案 0 :(得分:0)
首先改变
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
与
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
第二次不要返回0
if (o1.date== null || o2.date== null)
{
return 0;
}
return 0;
表示o1.date == o2.date
相反,您可以抛出异常,或设置默认日期。
更新:
我希望你的意思是什么。 valid date > string > null > ""
如果不只是更改值;
public class ListRowComparator implements Comparator<ListViewRow> {
private final SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
private final int VALID_DATE = 4;
private final int STRING_DATE = 3;
private final int NULL_DATE = 2;
private final int EMPTY_DATE = 1;
private int priorityLevel(String date){
if (date == null) return NULL_DATE;
if (date.equals("")) return EMPTY_DATE;
try {
format.parse(date);
return VALID_DATE;
} catch (ParseException e){
return STRING_DATE;
}
}
@Override
public int compare(ListViewRow o1, ListViewRow o2) {
int o1Priority = priorityLevel(o1.date);
int o2Priority = priorityLevel(o2.date);
if (o1Priority == VALID_DATE && o2Priority == VALID_DATE){
try {
return format.parse(o1.date).compareTo(format.parse(o2.date));
} catch (ParseException e){
// unreachable code
e.printStackTrace();
}
}
if (o1Priority == STRING_DATE && o2Priority == STRING_DATE){
return o1.date.compareTo(o2.date);
}
// else
return o1Priority - o2Priority;
}
}
答案 1 :(得分:0)
<强>更新强>
private class ListRowComparator implements Comparator<ListViewRow> {
private Date date1;
private Date date2;
@Override
public int compare(ListViewRow o1, ListViewRow o2) {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
try {
if (o1.date == null) o1.date = "01/01/1900";
date1 = format.parse(o1.date);
} catch (ParseException e) {
try {
date1 = format.parse("01/01/1900");
} catch (ParseException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
try {
if (o2.date == null) o2.date = "01/01/1900";
date2 = format.parse(o2.date);
} catch (ParseException e) {
try {
date2 = format.parse("01/01/1900");
} catch (ParseException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
return date1.compareTo(date2);
}
}