2
Good morning!
In my List
, when I do the ordering I would like the null values to be last.
I’ll show you an example to make it clear:
Code output (in this case the word would be the letter after the number):
SEM ORDENAÇÃO: 10c 2018-01-01 11b null 12a 2018-01-02 ORDENADO PELO NÚMERO: 10c 2018-01-01 11b null 12a 2018-01-02 ORDENADO PELA PALAVRA: 12a 2018-01-02 11b null 10c 2018-01-01 ORDENADO PELA DATA: 10c 2018-01-01 11b null 12a 2018-01-02
You can see that in ordering by date the null value was before a nonnull value, and I would like the null value to be last. Follow the source below:
Main class:
public class Main
{
public static void main (String[] args)
{
Objeto o1 = new Objeto();
Objeto o2 = new Objeto();
Objeto o3 = new Objeto();
o1.numero = 1;
o1.palavra = "c";
o1.data = LocalDate.of(2018, 01, 1);
o2.numero = 2;
o2.palavra = "b";
o3.numero = 3;
o3.palavra = "a";
o3.data = LocalDate.of(2018, 01, 2);
List<Objeto> objetos = new ArrayList<>();
objetos.add(o1);
objetos.add(o2);
objetos.add(o3);
System.out.println("SEM ORDENAÇÃO:\n");
for(Objeto objeto : objetos)
{
System.out.println(objeto.numero + '\t' + objeto.palavra + '\t' + objeto.data);
}
System.out.println("\n\nORDENADO PELO NÚMERO:");
Collections.sort(objetos, new ObjetoComparator(1));
for(Objeto objeto : objetos)
{
System.out.println(objeto.numero + '\t' + objeto.palavra + '\t' + objeto.data);
}
System.out.println("\n\nORDENADO PELa PALAVRA:");
Collections.sort(objetos, new ObjetoComparator(2));
for(Objeto objeto : objetos)
{
System.out.println(objeto.numero + '\t' + objeto.palavra + '\t' + objeto.data);
}
System.out.println("\n\nORDENADO PELA DATA::");
Collections.sort(objetos, new ObjetoComparator(3));
for(Objeto objeto : objetos)
{
System.out.println(objeto.numero + '\t' + objeto.palavra + '\t' + objeto.data);
}
}
}
Object Class:
public class Objeto
{
public int numero;
public String palavra;
public LocalDate data;
}
Comparison class:
public class ObjetoComparator implements Comparator<Objeto>
{
/*
* 1 - Compara pelo numero
* 2 - Compara pela palavra
* 3 - Compara pela data
*/
private int ord;
public ObjetoComparator(int Ord)
{
this.ord = Ord;
}
public int compare(Objeto o1, Objeto o2)
{
switch(this.ord)
{
case 1:
if (o1.numero < o2.numero)
return -1;
if (o1.numero > o2.numero)
return 1;
return 0;
case 2:
return o1.palavra.compareTo(o2.palavra);
case 3:
try
{
return o1.data.compareTo(o2.data);
}
catch(NullPointerException e)
{
return -1;
}
default:
return 0;
}
}
}
Yes, as the good practices I always use them. I only made these classes for example, so much so that it has no use at all. The important thing was the issue of ordination itself.
– Carlos Rafael de Oliveira Carn
The question of the nine I did not know, I found it strange not to leave the tab in the result. Thank you for this kkkk information and finally, thank you very much for the reply, helped a lot : -)
– Carlos Rafael de Oliveira Carn
I liked the two answers. But only for @Carlosrafaeldeoliveiracarn :)
– Maniero
One more question. Uppercase and lowercase interfere with String ordering?
– Carlos Rafael de Oliveira Carn
@Carlosrafaeldeoliveiracarn Yes, interfere. You can use the method
compareToIgnoreCase(String)
to solve this.– Victor Stafusa