0

I have an object Product that has the follow parameters:

String name;
int quantity;
String date;

where date is the toString of a GregorianCalendar object;

If I have an ArrayList<Product> myList, I want to return a new ArrayList of Product object sorted by date cronologically not alfabetically(I have already a method to convert String in GregorianCalendar but I don't know how use this in the compareTo method for sorting).

Since the list is not a simple ArrayList<String> and the sorting depends by an internal parameter in the ArrayList of object, how could I do this?

4 Answers 4

2

IMHO, it would be better to store date as a GregorianCalendar object and not as String.

Then just create a custom comparator and compare only the date field using its compareTo method (inherited from the Calendar class).

Collections.sort(myList, new Comparator<Product>() {
       @Override
       public int compare(Product p0, Product p1) {
           return p0.getDate().compareTo(p1.getDate());
       }
});
Sign up to request clarification or add additional context in comments.

8 Comments

I know, however I have a methopd to convert the GregorianCalendar and viceversa. how could I use this to sort cronologically using comparator?
@AndreaF You can call this method first to transform your String as a date object and then call compareTo, however it seems more suitable to store a date as a date and just give its String representation when you want to display it.
unfortunately the object stored have date as String and change this will cause problems in many method of the project
@AndreaF Then use this method in the compare method to convert the String as Date first for both and then call compareTo. Something like return myMethod(p0.getDate()).compareTo(myMethod(p1.getDate()));
but I don't have understand where exactly I have to place your piece of code
|
1

You can use Comparator to sort the array list of Product class

5 Comments

Could you give me more details?
The problem is the cronological order. I have a methopd to convert the GregorianCalendar to String and viceversa. how could I use this to sort cronologically using comparator (otherwise will be sorted alfabetically by date)?
If u want to convert Date into String and vice versa, you can go with SimpleDateFormat class
I yet have a method to convert String in GregorianCalendar but i don't know how shoud I use this in the compareTo
A GregorianCalendar already implements Comparable<Calendar> so you can just call the compareTo method of the calendar object to compare them.
0

if you add implements Comparable to your Product class declaration and add something like the following method

/**
 * 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.  
}

This will determine the default sort order for Collections of Products.

Comments

0

Keep the Date as GregorianCalendar or java.util.Date and Implement Comparable Interface in the Product class. So you need to override the method called 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 
    }
}

Use Collection.sort(list); whereever you need to sort the list of products based on date. For descending order, replace line 9 with return p1.date.compareTo(this.date);.

1 Comment

make it as Comparable<Product>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.