Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply sorting on date column: #189

Closed
sanakalam opened this issue Mar 20, 2020 · 8 comments
Closed

Apply sorting on date column: #189

sanakalam opened this issue Mar 20, 2020 · 8 comments
Assignees

Comments

@sanakalam
Copy link

I want to apply sorting on date column but there is an issue date is also considered as a string and 1st date of all months shown then 2nd date of all month showing.
How can we manage sorting for date column
image

@ISchwarz23 ISchwarz23 self-assigned this Mar 20, 2020
@ISchwarz23
Copy link
Owner

Hi @sanakalam,

can you pleas show your comparator for the date column?

Best regards,
Ingo

@sanakalam
Copy link
Author

tableView.setColumnComparator(6, new Comparator() {
@OverRide
public int compare(RequestInquiryModel o1, RequestInquiryModel o2) {
return o1.getCreatedOn().compareTo(o2.getCreatedOn());
}
});

@ISchwarz23
Copy link
Owner

What data type does getCreatedOn() return? String?

@sanakalam
Copy link
Author

yeah String. I am fetching data from API and date return as a String

@ISchwarz23
Copy link
Owner

I would suggest to transform the string to date using SimpleDateFormat.

@sanakalam
Copy link
Author

SimpleTableDataColumnAdapter c7 = new SimpleTableDataColumnAdapter<>((SimpleTableDataColumnAdapter.StringValueExtractor) data -> data.getCreatedOn().toString());

For Date which method we have to use instead of StringValueExtractor because it only accepts String value.

@ISchwarz23
Copy link
Owner

Hi you can use SimpleDateFormat to turn you Date into a String.

SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); // TODO: adapt pattern

SimpleTableDataColumnAdapter c7 = new SimpleTableDataColumnAdapter<>(
    (SimpleTableDataColumnAdapter.StringValueExtractor) data -> dateFormatter.format(data.getCreatedOn()));

@sanakalam
Copy link
Author

@ISchwarz23 Thanks For your Support.
I use below code it works:
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
tableView.setColumnComparator(6, new Comparator() {
@OverRide
public int compare(RequestInquiryModel o1, RequestInquiryModel o2) {
Date d1=null;
Date d2=null;
try {
d1=format.parse(o1.getCreatedOn());
d2=format.parse(o2.getCreatedOn());
} catch (ParseException e) {
e.printStackTrace();
}
return d1.compareTo(d2);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants