Sort by date and name Arrayadapter

Asked

Viewed 510 times

0

I am using "Comparator" to sort my listview. Sort only 1 element, it is working, but with two the same does not work.

 //ordenar por data
                        arrayAdapter.sort(new Comparator<Vagas>() {
                            @Override
                            public int compare(Vagas o1, Vagas o2) {
                                return o2.getDataAtualizacao().compareTo(o1.getDataAtualizacao());
                            }
                        });

And just below it I’m trying to order by name

    //ordenar por nome
                        arrayAdapter.sort(new Comparator<Vagas>() {
                            @Override
                            public int compare(Vagas o1, Vagas o2) {
                                return o1.getNome().compareTo(o2.getNome());
                            }
                        });

Theoretically it was not for him to sort by date and then order by name?

If I leave only sorting by name or by date, it orders normally.

1 answer

0


Theoretically it was not for him to order by date and then soon after sort by name?

Not simultaneously. This way in which you are doing, as you are using the method sort in the same Adapter, only an ordination had prevailed, which would be the last.

Making an adaptation of this answer, using 2 strings, you should look like this below, for example sort a array any through the Name and Surname. See:

arrayAdapter.sort(new Comparator <Vagas> () {
    @Override
    public int compare(Vagas o1, Vagas o2) {
        String x1 = ((Vagas) o1).getNome();
        String x2 = ((Vagas) o2).getNome();
        int sComp = x1.compareTo(x2);

        if (sComp != 0) {
            return sComp;
        } else {
            String y1 = ((Vagas) o1).getSobrenome();
            String y2 = ((Vagas) o2).getSobrenome();
            return y1.compareTo(y2);
        }
    }
});

Obs.: As well as that method getDataAtualizacao by name would be a date, so one should do the treatment if it is not in format Date. In the example above, it would be even compared in strings. For this case, you must be in format Date. Behold:

Date y1 = ((Vagas) o1).getDataAtualizacao();
Date y2 = ((Vagas) o2).getDataAtualizacao();
  • Is there any way to do this ordering, using the same Adapter? Or would you have to look for something else to do? If you can quote I thank you for being able to seek and learn

  • @Flávio corrected the problems that were, see now if it works correctly and tell me.

  • he comes to execute but he falls in if( sComp !=0) , then it orders only by name. If I change to if (sComp == 0) he orders by date

Browser other questions tagged

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