import java.util.Comparator;
import java.util.Date;
//Class that returns a string columnn in ascending order
public class DateCompare implements Comparator {
int columnToSort;
DateCompare(int columnToSort) {
this.columnToSort = columnToSort;
}
//overriding compare method
public int compare(Object o1, Object o2) {
Date[] row1 = (Date[]) o1;
Date[] row2 = (Date[]) o2;
//compare the columns to sort
return row1[columnToSort].compareTo(row2[columnToSort]);
}
}
My 2-D array stores phone names and their Dates of manufacture. Friends,I have made a class named DateCompare that sorts 2-D array of records based upon column that stores dates. This is not giving the desired output.Can anyone rectify this or give a better substitute solution because i couldn't find a clear answer on any site? Thanks in advance!!!
input(for example):
Samsung 01/01/2014
iphone 09/02/2006
Motorola 16/06/2009
Output:
iphone 09/02/2006
Motorola 16/06/2009
Samsung 01/01/2014
Why a 2D array, why not write a class to hold your data in a structured manner?
public class MyTuple implements Comparable<Date>
{
String phone;
Date date;
@Override
public int compareTo( Date d )
{
return d.compareTo( this.date );
}
}
and use a List<MyTuple>
instead of an array? I believe it was Joachim Sauer on this site that taught me the phrase "object denial" of which this is a good example :-)
Cheers,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.