How to order a list in alphabetical order?

Asked

Viewed 1,331 times

2

I own a List<ListaUsuarios> and I want to sort it alphabetically by name


My code is like this

User class:

public class Usuario{
   private String nome;
   private String empresa;
   private int    idade;
   private String email;

   public Usuario(String nome, String empresa, int idade, String email){
       this.nome    = nome;
       this.empresa = empresa;
       this.idade   = idade;
       this.email   = email;
   }

   //...GETs E SETs PARA TODAS AS VARIAVEIS GLOBAIS . . .
}

class Listausuario:

public class ListaUsuario{
    private Usuario user;
    private int     codigo;
    private int     nivel;
    private boolean ativo;


    public ListaUsuario(Usuario user, int codigo, int nivel, boolean ativo){
        this.user   = user;
        this.codigo = codigo;
        this.nivel  = nivel;
        this.ativo  = ativo;
    }

   //...GETs e SETs PARA TODAS AS VARIAVEIS GLOBAIS . . .
}

I create my Listin this way: List<ListaUsuario> = new Arraylist<>();

After that I populate her with N items of the type ListaUsuario


How can I order it in alphabetical order by User name?

  • 1

    There are several examples on the site.

  • Thank you, I’ll study on them!

1 answer

-1

You must want something like this example I believe

import java.util.Arrays;
import java.util.Collections;    

public class HashtableDemo {

  public static void main(String args[]) {
    String[] companies = { "Yahoo", "Vodafone", "Samsung" };

      System.out.println("Array de String Ordenada em Ordem Crescente: ");
      printNumbers(companies);
  }
}

Your exit will be as below

Samsung
Vodafone
Yahoo
  • It seems to me that the sorting occurs in printNumbers(Companies); it has to update the response with the scope of this method?

Browser other questions tagged

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