通过Object内的String参数对Object的ArrayList进行排序

时间:2014-02-27 18:19:32

标签: java android sorting arraylist

我有一个对象Product,其中包含以下参数:

String name;
int quantity;
String date;

其中dateGregorianCalendar对象的toString;

如果我有一个ArrayList<Product> myList,我想返回一个Product对象的新ArrayList,按date排序,按时间顺序不是alfabetically(我已经有一个方法来转换GregorianCalendar中的String但是我不知道如何在compareTo方法中使用它进行排序)。

由于列表不是简单的ArrayList<String>,并且排序取决于对象的ArrayList中的内部参数,我该怎么做?

4 个答案:

答案 0 :(得分:2)

恕我直言,最好将日期存储为GregorianCalendar对象,而不是String

然后只创建一个自定义比较器,并使用其compareTo方法(继承自Calendar类)仅比较日期字段。

Collections.sort(myList, new Comparator<Product>() {
       @Override
       public int compare(Product p0, Product p1) {
           return p0.getDate().compareTo(p1.getDate());
       }
});

答案 1 :(得分:1)

您可以使用Comparator对Product class

的数组列表进行排序

答案 2 :(得分:0)

如果您将Products Comparable添加到Product类声明中 添加类似以下方法的内容

/**
 * Compares this object with the specified object for order.  Returns a
 * negative integer, zero, or a positive integer as this object is less
 * than, equal to, or greater than the specified object.
 * <p/>
 * <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
 * -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
 * implies that <tt>x.compareTo(y)</tt> must throw an exception iff
 * <tt>y.compareTo(x)</tt> throws an exception.)
 * <p/>
 * <p>The implementor must also ensure that the relation is transitive:
 * <tt>(x.compareTo(y)&gt;0 &amp;&amp; y.compareTo(z)&gt;0)</tt> implies
 * <tt>x.compareTo(z)&gt;0</tt>.
 * <p/>
 * <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
 * implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
 * all <tt>z</tt>.
 * <p/>
 * <p>It is strongly recommended, but <i>not</i> strictly required that
 * <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>.  Generally speaking, any
 * class that implements the <tt>Comparable</tt> interface and violates
 * this condition should clearly indicate this fact.  The recommended
 * language is "Note: this class has a natural ordering that is
 * inconsistent with equals."
 * <p/>
 * <p>In the foregoing description, the notation
 * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
 * <tt>0</tt>, or <tt>1</tt> according to whether the value of
 * <i>expression</i> is negative, zero or positive.
 *
 * @param o the object to be compared.
 * @return a negative integer, zero, or a positive integer as this object
 *         is less than, equal to, or greater than the specified object.
 * @throws NullPointerException if the specified object is null
 * @throws ClassCastException   if the specified object's type prevents it
 *                              from being compared to this object.
 */
@Override
public int compareTo(Product o) {
 // add logic to compare this products date with the other's (o.getDate())
// return 1 if this products date is greater than the other's  and 
// return -1 if this product's date is less than the other's

   return 0; if they are the same.  
}

这将确定产品集合的默认排序顺序。

答案 3 :(得分:0)

将日期保持为GregorianCalendarjava.util.Date并在Product类中实现Comparable Interface。因此,您需要覆盖名为compareTo(Product p)的方法。

public class  Product implements Comparable<Product> {
    String name;
    int quantity;
    Date date;

    @override
    public int compareTo(Product p1){
      return this.date.compareTo(p1.date); //line 9 for ascending order 
    }
}

使用Collection.sort(list);您需要根据日期对产品列表进行排序。对于降序,请将第9行替换为return p1.date.compareTo(this.date);