I can not make a sort from an arrayList of objects - Java

0

I have to show a% co_of% of person object by the name of Arraylist . The information I find is with a A-Z but it is not implemented. my code is like this:

Person class

import java.util.GregorianCalendar;

public class Persona {

private String nombre, apellido, dni;
private GregorianCalendar fechaNaci;

public Persona(String nombre, String apellido, String dni, GregorianCalendar fechaNaci) {
    this.nombre = nombre;
    this.apellido = apellido;
    this.dni = dni;
    this.fechaNaci = fechaNaci;
}



public String getNombre() {
    return nombre;
}

public String getApellido() {
    return apellido;
}

public String getDni() {
    return dni;
}

public GregorianCalendar getFechaNaci() {
    return fechaNaci;
}

@Override
public String toString() {
    return nombre + "\t\t" + apellido + "\t\t" + dni + "\t" + fechaNaci.get(fechaNaci.DAY_OF_MONTH) +"/"+fechaNaci.get(fechaNaci.MONTH) +"/"+fechaNaci.get(fechaNaci.YEAR);
}

Application menu I enter the data automatically to do the tests.

class Pers {

    public static void main(String[] args) {

        ArrayList<Persona> arPersona = new ArrayList<Persona>();

        arPersona.add(new Persona("Brian", "Flores Maya asd", "76587515M", new GregorianCalendar(1995, 11, 06)));
        arPersona.add(new Persona("Samu", "Martin Martin", "34509475F", new GregorianCalendar(1996, 8, 11)));
        arPersona.add(new Persona("Arthur", "Alcovendo", "82340945D", new GregorianCalendar(1997, 03, 23)));
        arPersona.add(new Persona("Josep", "Torrent Garcia", "98734561L", new GregorianCalendar(1993, 12, 14)));

        int opcion = 0;
        do {
            opcion = menu();
            switch (opcion) {
                case 1:
                    arPersona.add(introduirPersona());
                    break;
                case 2:

                    ordenarAZ(arPersona);
                case 3:

                    break;
                case 4:

                    break;
                case 5:

                    break;
                case 6:

                    break;
                case 7:

                    break;
                case 8:

                    break;
                case 9:

                    break;
                case 0:
                    break;
                default:
                    System.out.println("Opcio incorrecta");
                    break;
            }
        } while (opcion != 0);

    }//fin main

    public static void ordenarAZ(ArrayList<Persona> arPersonas) {
        //Mostrar les persones ordenades per nom (A-Z).





       // Collections.sort(arPersonas.get());
        for (Persona x : arPersonas) {

            System.out.println(x.toString());
        }

    }//fin ordenarAZ

    public static Persona introduirPersona() {
        String nom = Teclado.leerString("Introdueix el nom: ");
        String cognom = Teclado.leerString("Introdueix el cognom: ");
        String dni = Teclado.leerString("Introdueix el DNI: ");
        GregorianCalendar data = Teclado.leerData("Introdueix una data: ");

        Persona persona = new Persona(nom, cognom, dni, data);

        return persona;
    }//fin intorduirPersona

    public static int menu() {
        int opcion = 0;
        System.out.println("1.- Introduïr persona\n"
                + "2.- Mostrar les persones ordenades per nom (A-Z).\n"
                + "3.- Mostrar les persones ordenades per nom (Z-A).\n"
                + "4.- Mostrar les persones ordenades per cognoms (A-Z).\n"
                + "5.- Mostrar les persones ordenades per cognoms (Z-A).\n"
                + "6.- Mostrar les persones ordenades per NIF (1-9).\n"
                + "7.- Mostrar les persones ordenades per NIF (9-1).\n"
                + "8.- Mostrar les persones ordenades per data de naixement (1-9).\n"
                + "9.- Mostrar les persones ordenades per data de naixement (9-1).\n"
                + "0. Sortir");
        opcion = Teclado.leerInt("Opcio: ");
        return opcion;
    }//fin menu

}

How can I order the sort of people from A to Z?

    
asked by Brian Flores 19.02.2018 в 20:14
source

2 answers

0

If you are allowed or use Java 8 , you can use the Comparator interface to make the comparison more readable. Concatenating the comparisons with thenComparing () with the attributes you want. (1 or more than 1) this after comparing ()

arPersona.sort(Comparator.comparing(Persona::getApellido)
        .thenComparing(Persona::getNombre));
    
answered by 19.02.2018 / 21:55
source
1

If you need to sort by first and last name, I recommend that in your class you implement the interface Comparable and compare by these fields:

public class Persona implements Comparable {
  ...

  @Override
  public int compareTo(Persona other) {
    int res = this.apellido.compareTo(other.apellido);
    if (res != 0) // Si los apellidos son iguales, compara por nombre
      res = this.nombre.compareTo(other.nombre);
    return res;
  }
}

And then you simply use:

Collections.sort(arPersonas);
    
answered by 19.02.2018 в 21:11