Created
March 1, 2023 18:16
-
-
Save ulisseshen/69ee040125b56f1c44c67c2af0333336 to your computer and use it in GitHub Desktop.
Ordenação de nome e idade DESC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
// códigos anteriores aqui | |
pessoas.sort((a, b) { | |
// Primeiro, comparar pelo campo nome | |
int comparacao = -a.nome.compareTo(b.nome); | |
if (comparacao != 0) { | |
return comparacao; | |
} | |
// Se os nomes forem iguais, comparar pelo campo idade | |
// adicionamos o sinal de menos para ser Desc | |
return -a.idade.compareTo(b.idade); | |
}); | |
print("---- pós sort nome desc, idade desc---"); | |
pessoas.forEach(print); | |
// ---- pós sort nome desc, idade desc--- | |
// Pedro, 23 | |
// Pedro, 20 | |
// Maria, 37 | |
// Maria, 34 | |
// Maria, 18 | |
// Lucas, 25 | |
// Lucas, 15 | |
// João, 25 | |
// João, 18 | |
// João, 17 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment