Sort by date does not work - Firestore

Asked

Viewed 73 times

0

Hello, I have a variable in the database that contains the date in "dd/MM/yyyy" format, when I try to use the orderby of a query with that date firebase uses only the days (dd) to sort my recyclerView, making the query is not sorted efficiently, How can I get around this problem? There is no way to remove the "/" after they are in the database, as the method . orderby requires as parameter a field value that is in the database (existing there (22/04/1947)) and not a local value, I saw that it is possible to use Timestamp, but if I use timestamp, I would show my user "144546541", which is not anything readable...

Ordering example: anuncioRef.orderBy(campo, Query.Direction.ASCENDING).limit(100);, where field is a value stored in the Database and that is not changed here, field here is the date value (dd/MM/yyyy)

  • You are storing your date in string format or using the data type?

  • Can’t change this string to another format in the database? example: "yyyyMMdd" or "yyyy-MM-dd" would be -"19470422" or "1947-04-22". In this format you could order.

  • Storing as String, there is no way to store as date because of //

  • The problem is that if I change to this format, when displaying the ads I have to add the / manually, right?

1 answer

-1

After getting the database date, I formatted it by following this tutorial

and the code was like this in my Recyclerview:

 String data = anuncioPrincipal.getDataAnuncio();
        SimpleDateFormat formInicio = new SimpleDateFormat("ddMMyyyy");
        SimpleDateFormat formFim = new SimpleDateFormat("dd/MM/yyyy");
        try {
            data = formFim.format(formInicio.parse(data));
        } catch (ParseException e) {
            e.printStackTrace();
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.