I have a list of 97 elements that i need to sot on basis of date which contains dates of format DD/MM/YYYY which is in string type only, null objects and objects of String type also, i.e. it has elements such as
我有一个97个元素的列表,我需要根据日期进行sot,其中包含格式为DD / MM / YYYY的日期,仅为字符串类型,null对象和String类型的对象,即它具有如下的元素:
Example of data in my date field :
我的日期字段中的数据示例:
12/02/2014
11/07/2017
null
India
Now I want to sort the elements of my list based on date field.
现在我想根据日期字段对列表中的元素进行排序。
what i want is that all the entries with date should apear first in ascending order then all the entries with strings such as india then entries with null in date field then entries with empty date field, so how should i code it in compareTo method
我想要的是所有带日期的条目应首先以升序排列,然后所有带有字符串的条目(如india)然后在日期字段中带有null的条目然后带有空日期字段的条目,那么我应该如何在compareTo方法中对其进行编码
I have seen a lot of examples on that but not getting the desired result. How to do that?Help me please?
我已经看到很多例子,但没有得到理想的结果。怎么做?请帮帮我?
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;
}
}
2 个解决方案
#1
0
Update:
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);
}
}
#2
0
First change
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
with
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Second don't return 0
第二次不归0
if (o1.date== null || o2.date== null)
{
return 0;
}
return 0;
means o1.date == o2.date
返回0;表示o1.date == o2.date
Instead you can throw an exception, or set a default date.
相反,您可以抛出异常,或设置默认日期。
UPDATE:
I hope that's what you mean. valid date > string > null > ""
if not just change the value;
我希望这就是你的意思。有效日期>字符串> 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
Update:
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);
}
}
#2
0
First change
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
with
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Second don't return 0
第二次不归0
if (o1.date== null || o2.date== null)
{
return 0;
}
return 0;
means o1.date == o2.date
返回0;表示o1.date == o2.date
Instead you can throw an exception, or set a default date.
相反,您可以抛出异常,或设置默认日期。
UPDATE:
I hope that's what you mean. valid date > string > null > ""
if not just change the value;
我希望这就是你的意思。有效日期>字符串> 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;
}
}