I am doing a class assignment and I have to order a arraylist
by date, I have implemented the comparable class but the truth is that I don't know how to create the compare to.
The work report class has: Customer name, customer address, date of the report, name of the worker who is going to do it, status ((P)pending/(R)done), expected date of repair, time spent in minutes , description of the arrangement specifically asks me to Sort the work reports by the date of the report in ascending order. I leave you my code:
public class ParteDeTrabajo {
private String nombreCliente;
private LocalDate fechaParte;
private String nombreTrabajador;
private char estado;
private LocalDate fechaReparacion;
private double tiempoReparacion;
private String descripcionArreglo;
private ArrayList<Material> materiales;
public String getNombreCliente() {
return nombreCliente;
}
public void setNombreCliente(String nombreCliente) {
this.nombreCliente = nombreCliente;
}
public LocalDate getFechaParte() {
return fechaParte;
}
public void setFechaParte(LocalDate fechaParte) {
this.fechaParte = fechaParte;
}
public String getNombreTrabajador() {
return nombreTrabajador;
}
public void setNombreTrabajador(String nombreTrabajador) {
this.nombreTrabajador = nombreTrabajador;
}
public char getEstado() {
return estado;
}
public void setEstado(char estado) {
if (this.estado == 'p' || this.estado == 'R') {
this.estado = estado;
}
}
public LocalDate getFechaReparacion() {
return fechaReparacion;
}
public void setFechaReparacion(LocalDate fechaReparacion) {
this.fechaReparacion = fechaReparacion;
}
public double getTiempoReparacion() {
return tiempoReparacion;
}
public void setTiempoReparacion(double tiempoReparacion) {
if (this.tiempoReparacion >= 0) {
this.tiempoReparacion = tiempoReparacion;
}
}
public String getDescripcionArreglo() {
return descripcionArreglo;
}
public void setDescripcionArreglo(String descripcionArreglo) {
this.descripcionArreglo = descripcionArreglo;
}
public ArrayList<Material> getMateriales() {
return materiales;
}
public void setMateriales(ArrayList<Material> materiales) {
this.materiales = materiales;
}
public ParteDeTrabajo(String nombreCliente, LocalDate fechaParte, String nombreTrabajador, char estado, LocalDate fechaReparacion, double tiempoReparacion, String descripcionArreglo, ArrayList<Material> materiales) {
this.nombreCliente = nombreCliente;
this.fechaParte = fechaParte;
this.nombreTrabajador = nombreTrabajador;
setEstado(estado);
this.fechaReparacion = fechaReparacion;
setTiempoReparacion(tiempoReparacion);
this.descripcionArreglo = descripcionArreglo;
this.materiales = materiales;
}
public ParteDeTrabajo() {
this.nombreCliente = "";
this.fechaParte = null;
this.nombreTrabajador = "";
this.estado = 'p';
this.fechaReparacion = null;
this.tiempoReparacion = 0;
this.descripcionArreglo = "";
this.materiales = new ArrayList<Material>();
}
public String aniadirMateriales(Material a) {
materiales.add(a);
return "material introducidos";
}
@Override
public String toString() {
String str = "";
str += "Parte de trabajo" + "\n";
str += "fecha del parte: " + fechaParte + "\n nombre del cliente: " + nombreCliente + "\n Atendido por: " + nombreTrabajador
+ "\n estado del parter: " + estado + "\n fecha de reparacion: " + fechaReparacion + "\n se ha tardado: " + tiempoReparacion
+ "minutos \n ";
for (Material m : materiales) {
str += m.toString();
}
return str;
}
}
The Comparable interface is used, as its name suggests, to make the objects of a class comparable to each other. Therefore, it is the elements that you keep in the collections that you want to order that have to comply with it.
You would have to do something like:
And then you just have to sort your list:
A compareTo method can return three values: 1 if a > b , -1 if a < b , 0 if a = b