2
I am creating a register of employees with some fields(name, email, phone and salary) in ArrayList
which are recorded in txt.
After the data is entered into the array (before passing to txt) they need to be sorted by name, which is the first field from left to right.
And this is where the problem is, because I can’t use any ready method of sorting type Sort. I have to create a quicksort.
I’ve tried a lot of ways, and I couldn’t. I thought to try with the string compare, but I don’t know how he could do to sort alphabetically.
Will it work? Can someone give me a light?
Main class:
public static void main(String[] args) throws IOException
{
ArrayList<Funcionario> funcionarios = new ArrayList<Funcionario>();
... menu do programa..
case 1:
Funcionario func = new Funcionario();
func.cadastrar();
funcionarios.add(func);
System.in.read();
System.out.println("\n\nFuncionario cadastrado: " + func.toString());
break;
Register class
public void cadastrar()
{
System.out.println("\n\n\n\n\n\n\n\n\n----- Cadastro de funcionário -----");
System.out.print("\nInforme o nome: ");
String nome = ler.nextLine();
this.setNome(nome);
System.out.print("\nInforme o e-mail: ");
String email = ler.nextLine();
this.setEmail(email);
boolean valida;
do
{
System.out.print("\nInforme o telefone: ");
String telefone = ler.nextLine();
if(telefone.length() != 10)
{
System.out.println("Erro!!\nO formato exige 10 dígitos\n");
valida = false;
}
else
{
valida = true;
this.setTelefone(telefone);
}
}while(!valida);
do
{
System.out.print("\nInforme o salário: R$ ");
float salario = ler.nextFloat();
if(salario <= 0)
{
System.out.println("ERRO!! Valor inválido");
valida = false;
}
else
{
valida = true;
this.setSalario(salario);
}
}while(!valida);
PrintWriter gravarTxt = new PrintWriter(arquivo);
gravarTxt.printf("\r\n"+this.nome+" "+this.email+" "+this.telefone+" R$ "+this.salario);
gravarTxt.flush();
}
Hi Maximilian, all right? It’s easier to help you if you create one mvce demonstrating exactly where your problem is. The method
compare
is useful for comparing lexicographic order yes (it can replace the part that would be made with comparison operators>
,<
in the case of types such asint
).– Anthony Accioly
Thanks for the touch, Anthony. Fixed
– Evilmaax
Check this out, see if you can understand and apply your http://stackoverflow.com/questions/31377448/how-to-sorting-in-array-list-without-using-collections-in-javacode
– user28595