我有一个对象Product
,其中包含以下参数:
String name;
int quantity;
String date;
其中date
是GregorianCalendar
对象的toString;
如果我有一个ArrayList<Product>
myList,我想返回一个Product
对象的新ArrayList,按date
排序,按时间顺序不是alfabetically(我已经有一个方法来转换GregorianCalendar中的String但是我不知道如何在compareTo方法中使用它进行排序)。
由于列表不是简单的ArrayList<String>
,并且排序取决于对象的ArrayList
中的内部参数,我该怎么做?
答案 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)>0 && y.compareTo(z)>0)</tt> implies
* <tt>x.compareTo(z)>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)
将日期保持为GregorianCalendar
或java.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);
。