Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
Sorting an arraylist with lambda function isn't working

I'm trying to use .sort() method with an arraylist.
My method public void sortSurname() should sort alphabetically all the objects by their surname, these objects are contained in an arraylist ArrayList<Contact> contacts.
Could someone please find the issue with this code?
Here is what I've written so far:
Contact class

package main;
public class Contact {
    private String name, surname, cellphone;
    private Genere genere;
    
    public Contact(String name, String surname, String cellphone){
        this.name=name;
        this.surname=surname;
        this.cellphone=cellphone;
    }

    public String getSurname() {
        return surname;
    }
}

Rubrica class

public class Rubrica {
    private ArrayList<Contact> contacts;
    
    public Rubrica(){
        contacts = new ArrayList<>();
    }

    public void sortSurname(){
        contacts.sort((Contact c1, Contact c2) -> {
            c1.getSurname().compareTo(c2.getSurname());
        });
    }

    public void addContact(Contact c1){
        contacts.add(c1);
    }
}

Main

package main;

public class Main {
public static void main(String[] args) {
    Contact c1 = new Contact("Paolo", "Groviera", "338");
    Contact c2 = new Contact("Paolo", "Groviera", "234");
    Contact c3 = new Contact("Lampa", "Dino", "234");
    Contact c4 = new Contact("Lampa", "Dina", "0234");
    
    Rubrica r = new Rubrica();
    r.addContact(c1);
    r.addContact(c2);
    r.addContact(c3);
    r.addContact(c4);

    r.sortSurname();
    System.out.println(r);
    }
}

Thank you for the help.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The problem is the {} which is a block and requires a return. Try it like this:

contacts.sort((Contact c1, Contact c2) -> {
   return  c1.getSurname().compareTo(c2.getSurname());
});

or forget the {} and just do

contacts.sort((Contact c1, Contact c2) -> 
    c1.getSurname().compareTo(c2.getSurname()));

or use the Comparator interface and pass a method reference.

contacts.sort(Comparator.comparing(Contact::getSurname));
over 4 years ago · Santiago Trujillo Denunciar

0

Get rid of the { }

public void sortSurname(){
    contacts.sort((Contact c1, Contact c2) -> 
        c1.getSurname().compareTo(c2.getSurname());
    );
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda