Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!