【问题标题】:How to sort my list on basis of date field如何根据日期字段对我的列表进行排序
【发布时间】:2017-11-06 05:50:02
【问题描述】:

我有一个包含 97 个元素的列表,我需要根据日期进行排序,其中包含格式为 DD/MM/YYYY 的日期,它仅是字符串类型,空对象和字符串类型的对象,即它具有这样的元素作为

我的日期字段中的数据示例:

12/02/2014
11/07/2017
null
India

现在我想根据日期字段对列表中的元素进行排序。

我想要的是所有带有日期的条目应该首先按升序出现,然后是所有带有字符串的条目,例如印度,然后是日期字段中为空的条目,然后是日期字段为空的条目,所以我应该如何在 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;
    }
}

【问题讨论】:

  • new SimpleDateFormat("dd-MM-yyyy");更改为new SimpleDateFormat("dd/MM/yyyy");
  • @chandil03 同意。

标签: java android sorting date listview


【解决方案1】:

更新:

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);
    }
}

【讨论】:

  • 但您在 Comparator 中采用 String 数据类型,因此它只会对日期字段进行排序,而对其他字段进行排序
【解决方案2】:

第一次改变

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 &gt; string &gt; null &gt; "" 如果不只是更改值;

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;
    }
}

【讨论】:

  • 我想要的是所有带有日期的条目都应该首先按升序出现,然后是所有带有字符串的条目,例如印度,然后是日期字段中为空的条目,然后是日期字段为空的条目,那么应该如何我在 compareTo 方法中编码它
  • @Ankit 我已经更新了我的答案,希望这就是你想要的
猜你喜欢
  • 2018-03-07
  • 2021-03-07
  • 1970-01-01
  • 2021-06-29
  • 2013-04-20
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多