java – Arrange list based on an integer value of its elements

Question:

I have an ArrayList of my class that contains String variables and integers, like a name and punctuation. When I display it through an Adapter would like the list to be scored in descending order, but I have not found how to do that. Does anyone know how? In case the text is the name, the punctuation is the approval of the comment.

Below is the part where I create the list and display it:

public class F_ListaComentarios extends ListFragment {

    C_Comentario comentario;
    private List<C_Comentario> comentarios;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        comentarios = new ArrayList<C_Comentario>();

        preencherListaComentarios(comentarios);

        if (comentarios != null) {
            adapter = new P_ComentarioAdapter(getActivity(), comentarios);
            setListAdapter(adapter);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.f_lista_comentarios, null, false);
        return (view);
    }

    public void preencherListaComentarios(ArrayList<C_Comentario> comentarios) {

        C_Comentario comentario = new C_Comentario();
        comentario.setTexto("AA");
        comentario.setAprovado(0);
        comentarios.add(comentario);

        C_Comentario comentario2 = new C_Comentario();
        comentario.setTexto("AV");
        comentario.setAprovado(2);
        comentarios.add(comentario);

        C_Comentario comentario3 = new C_Comentario();
        comentario.setTexto("AA");
        comentario.setAprovado(1);
        comentarios.add(comentario);

        C_Comentario comentario4 = new C_Comentario();
        comentario.setTexto("AA");
        comentario.setAprovado(5);
        comentarios.add(comentario);
    }
}

I managed to solve this by implementing Comparable<C_Comentario> to my C_Comentario class and adding

public static final Comparator<C_Comentario> DESCENDING_COMPARATOR = new Comparator<C_Comentario>() {
        // Overriding the compare method to sort the age
        public int compare(C_Comentario c, C_Comentario c1) {
            return c1.aprovado - c.aprovado;
        }
    };

e

@Override
    public int compareTo(C_Comentario c) {
        return (this.titulo).compareTo(c.titulo);
    }
}

the class too.

In the fragment I just needed to call

Collections.sort(comentarios, C_Comentario.DESCENDING_COMPARATOR);

Answer:

Use the Collections.sort() method passing its ArrayList and a Comparator<T> object as arguments. Collections itself provides the Collections.reverseOrder .

import java.util.ArrayList;
import java.util.Collections;

// ...
ArrayList<Integer> lista = new ArrayList<>();
lista.add(432);
lista.add(23);
lista.add(18);
lista.add(2);
lista.add(100);

Collections.sort(lista, Collections.reverseOrder());
System.out.println(lista); // [432, 100, 23, 18, 2]

Example working on Ideone

Scroll to Top