Sort objects according to name attribute using Collections.Sort() or Reverse()

Asked

Viewed 634 times

1

How to sort objects through the name attribute? I am implementing the Comparator interface.

I made a small example.

Let’s go to the codes:

file: Person.java

import java.util.ArrayList;  
import java.util.List;  

public abstract class Pessoa {  
    protected String nome;  
    protected int telefone;  
    protected int matricula;  
    private static int contadorMatricula;  

    private static int atribuirMatricula() {  
        Pessoa.contadorMatricula++;  
        return Pessoa.contadorMatricula;  
    }  

}  

file: Personal Physics.java

public class PessoaFisica extends Pessoa {  
    protected int cpf;  

    public PessoaFisica(String nome, int telefone, int cpf) {  
        this.nome=nome;  
        this.telefone=telefone;  
        this.cpf=cpf;  
    }  

}  

main file: Main.java

import java.util.ArrayList;  
import java.util.Collections;  
import java.util.List;  
import java.util.Comparator;  

public class Main implements Comparator<PessoaFisica> {  

    @Override  
    public int compare(PessoaFisica pessoa1, PessoaFisica pessoa2) {  
        return pessoa1.nome.compareTo(pessoa2.nome);  
        }  

    public static void main(String args[]) {  
        List pessoasFisicas = new ArrayList<>();  

        PessoaFisica pessoa1=new PessoaFisica("André Nascimento", 321, 654);  
        pessoasFisicas.add(pessoa1);  

        PessoaFisica pessoa2=new PessoaFisica("Tiago Santos", 123, 456);  
        pessoasFisicas.add(pessoa2);      
    }      
}  

As I show to the Collections.sort which is to order by name?

Note: I know you do not need to implement the Comparator interface, because the correct is to instantiate and already implement at runtime, example: new Comparator<PessoaFisica>();

but I’m doing it the least ideal way to facilitate my understanding.

  • 1

    Format that code there young.

  • 1

    That one answer should help.

2 answers

1

import java.util.*; 
import java.lang.*; 
import java.io.*; 

// A class to represent a student. 
public class Student {

    int rollno; 
    String name;
    String address; 

    // Constructor 
    public Student(int rollno, String name, String address) { 
        this.rollno = rollno; 
        this.name = name; 
        this.address = address; 
    } 

    // Used to print student details in main() 
    public String toString() { 
        return this.rollno + " " + this.name + " " + this.address; 
    } 
} 
//Aqui é o que falta para você
class Sortbyroll implements Comparator<Student> { 
    // Used for sorting in ascending order of roll number 
    public int compare(Student a, Student b) { 
        return a.rollno - b.rollno; 
    } 
} 
class Main {

    public static void main (String[] args) {

        ArrayList<Student> ar = new ArrayList<Student>(); 
        ar.add(new Student(111, "bbbb", "london")); 
        ar.add(new Student(131, "aaaa", "nyc")); 
        ar.add(new Student(121, "cccc", "jaipur")); 

        System.out.println("Sem ordenar"); 
        for (int i=0; i<ar.size(); i++){ 
            System.out.println(ar.get(i)); 
        } 

        Collections.sort(ar, new Sortbyroll()); 

        System.out.println("Ordenado por rollno"); 
        for (int i=0; i<ar.size(); i++){ 
            System.out.println(ar.get(i)); 
        }
    } 
} 

Source

0

You implemented the Comparator in Main, right?

Then you must pass to sort that the class Main is responsible for ordering!

As you are in a static method, you should instantiate the class Main:

  Main main = new Main();

 Collections.sort(pessoasFisicas, main);

There is a way to leave the sorting by name as default!

For this instead of implementing in the Class Main, implement in the Class PessoaFisica the interface Comparable :

 public class PessoaFisica extends Pessoa implements Comparable<PessoaFisica> {  

    …

    @Override

    public int compareTo(PessoaFisica o) {

        return nome.compareTo(o.nome);

    }  

}

Then you can call as follows :

Collections.sort(pessoasFisicas);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.