Sort by date and name ArrayAdapter

0

I'm using "Comparator" to sort my listview. Sort only 1 element, is working, but with 2 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 right below it I'm trying to sort 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 to sort by name?

If I just leave sorting by name or by date, it sorts normally.

    
asked by anonymous 27.06.2017 / 21:43

1 answer

0
  

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

Simultaneously, no. In this way you are doing, since you are using the sort method in the same adapter , only an ordering would be the last one.

Making a adaptation of this answer , using 2 strings, you should look like this below, for > array any through First Name and Last Name. 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);
        }
    }
});

Note: Since the getDataAtualizacao method by name would be a date, then you should do the treatment if it is not in the Date format. In the example above, it would even compare in strings. For this case, you should be in Date format. See:

Date y1 = ((Vagas) o1).getDataAtualizacao();
Date y2 = ((Vagas) o2).getDataAtualizacao();
    
27.06.2017 / 21:52