Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm about 90% sure you're comparing <code>java.lang.String</code>s and not <code>java.util.Date</code>s. Given the date format you mentioned, it'll obviously sort all the days together first. E.g. for a full year, your list would start off:</p> <pre><code>01-01-2012 (1 Jan) 01-02-2012 (1 Feb) 01-03-2012 (1 Mar) </code></pre> <p>Use the Date object to compare on, not a String representation of it.</p> <p>P.S. a picture is not code. Put your code in your question.</p> <p><strong>Update:</strong> Proof that the code you've posted works as expected:</p> <pre><code>import java.text.DateFormat; import java.util.*; public class DateComparators { public static void main(String[] args) { List&lt;Product&gt; productList = Arrays.asList( new Product(new Date(2012 - 1900, 5, 1)), // 1 Jun 2012 new Product(new Date(2012 - 1900, 1, 1)), // 1 Feb 2012 new Product(new Date(2012 - 1900, 2, 15)), // 15 Mar 2012 new Product(new Date(2012 - 1900, 0, 2)), // 2 Jan 2012 new Product(new Date(2012 - 1900, 1, 2)), // 2 Feb 2012 new Product(new Date(2012 - 1900, 0, 1)) // 1 Jan 2012 ); Collections.sort(productList, new CustomComparator()); System.out.println(productList); } static class CustomComparator implements Comparator&lt;Product&gt; { @Override public int compare(Product o1, Product o2) { return o1.getUseBy().compareTo(o2.getUseBy()); } } static class Product { private String productName; // sauce pan private Date boughtOn; // 4-5-2012 private Date useBy; // 4-5-2012 private double boughtAt; // $4.05 private long quantity; // 9 private static DateFormat dateFormat; Product(Date useBy) { this.useBy = useBy; } public Date getUseBy() { return useBy; } public void setUseBy(Date useBy) { this.useBy = useBy; } @Override public String toString() { return "\nuseBy=" + useBy; } } } </code></pre> <p>Output:</p> <pre><code>[ useBy=Sun Jan 01 00:00:00 CST 2012, useBy=Mon Jan 02 00:00:00 CST 2012, useBy=Wed Feb 01 00:00:00 CST 2012, useBy=Thu Feb 02 00:00:00 CST 2012, useBy=Thu Mar 15 00:00:00 CDT 2012, useBy=Fri Jun 01 00:00:00 CDT 2012] </code></pre> <p>Therefore, you haven't shown us everything. The problem is in something you left out.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload