package Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;

import djondb.BSONObj;

public class ReceiptDateComparator implements Comparator<BSONObj>{
	private String _order;
	private String _field;
	public ReceiptDateComparator(final String order, final String field)
	{
		_order = order;
		_field = field;
	}
	@Override
	public int compare(BSONObj o1, BSONObj o2) {
		// TODO Auto-generated method stub
		String n1 = o1.getString(_field);
		String n2 = o2.getString(_field);
		try {
			Date date1 = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH).parse(n1);
			Date date2 = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH).parse(n2);

			if(_order.equalsIgnoreCase("asc"))
			{
				return date1.compareTo(date2);
			}
			else
			{
				return date2.compareTo(date1);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			if(_order.equalsIgnoreCase("asc"))
			{
				return n1.compareTo(n2);
			}
			else
			{
				return n2.compareTo(n1);
			}
		}
		
	}

}
