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