Sort arraylist by two parameters

1

I have java a class Mensaje with the attributes:

String Tipo
int Orden
String Contenido

In a ArrayList I add several instances of this class

The problem arises when I have to order it in such a way that I stay above sorted alphabetically by type and in order, I tested with compareTo and Collections.sort() and they do not work at all. I need help

Example of result:

Before ordering

Tipo Orden Contenido
E    6     aaa
R    2     aaa
E    9     aaa
R    10    aaa

After ordering

Tipo Orden Contenido
R    2     aaa
R    10    aaa
E    6     aaa
E    9     aaa

Message class:

public class Mensaje {
    int Tipo;
    int Orden;
    String Contenido;

    public Mensaje(int i, int j, String string) {
        Tipo = i;
        Orden = j;
        Contenido = string;     
    }
    public int getTipo() {
        return Tipo;
    }
    public void setTipo(int tipo) {
        Tipo = tipo;
    }
    public int getOrden() {
        return Orden;
    }
    public void setOrden(int orden) {
        Orden = orden;
    }
    public String getContenido() {
        return Contenido;
    }
    public void setContenido(String contenido) {
        Contenido = contenido;
    }
}

Main:

public class Main {
    static ArrayList<Mensaje> mensajes = new ArrayList<Mensaje>();

    public static void main(String[] args) {
        mensajes.add(new Mensaje(1, 2, "P01002"));
        mensajes.add(new Mensaje(0, 6, "P01002"));
        mensajes.add(new Mensaje(1, 5, "P01002"));
        mensajes.add(new Mensaje(0, 3, "P01002"));
        imprimir(mensajes);
    }
    private static void imprimir(ArrayList<Mensaje> a){
        for(Mensaje m : a){
            System.out.println(m.getTipo() + " " + m.getOrden() + " " + m.getContenido());
        }
    }
}

The problem is that I do not know what kind it puts as comparable etc ... and the internet examples do not understand them

    
asked by Karveg 27.03.2018 в 16:00
source

2 answers

1

You can implement Comparable to define how to compare two instances and then Collections.sort(list) will use your method to compare. For example, to sort first by Tipo and then by Orden :

public class Mensaje implements Comparable {
    //...
@Override
public int compareTo(Object o) {
    Mensaje otro = (Mensaje) o;
    int commparacionPorTipo = Tipo.compareTo(otro.getTipo());

    if (commparacionPorTipo != 0)
        return commparacionPorTipo;

    return Integer.compare(Orden, otro.getOrden());
}
}
    
answered by 27.03.2018 / 16:44
source
0

Whether you implement it with the Comparable<T> interface or by providing a Comparator<T> to Collections.sort() you must define an order to compare the attributes.

Assuming 2 objects m1 and m2 of class Mensaje

  • If you first order by type then you must perform:

    int resultado = m1.getTipo().compareTo(m2.getTipo());
    
  • If the result is different from 0, then you return it.

    if(resultado != 0) {
        return resultado;
    }
    
  • If it is equal to 0, then they have the same Tipo and you should compare by the following attribute: Orden :

    return m1.getOrden().compareTo(m2.getOrden());
    

Both interfaces Comparable<T> and Comparator<T> return int which is not more than the relative position between 2 objects:

  • < 0 if the first one is before the second one
  • = 0 if they have the same position
  • > 0 if the first is after the second
answered by 27.03.2018 в 16:28