Dropdown Ordering Angular4 [closed]

-1

Hello, I have a state field (UF) that is as a dropdown and would like to sort it (by name). Could you please give me a map? My code looks like this today:

  estados = [
{label: 'Acre', value: 'AC'}, {label: 'Amazonas', value: 'AM'}, {label: 'Amapa', value: 'AP'},
{label: 'Pará', value: 'PA'}, {label: 'Roraima', value: 'RR'}, {label: 'Rondonia', value: 'RO'},
{label: 'Tocantins', value: 'TO'}, {label: 'Rio Grande do Sul', value: 'RS'}, {label: 'Santa Catarina', value: 'SC'},
{label: 'Paraná', value: 'PR'}, {label: 'São Paulo', value: 'SP'}, {label: 'Rio de Janeiro', value: 'RJ'},
{label: 'Minas Gerais', value: 'MG'}, {label: 'Espirito Santo', value: 'ES'}, {label: 'Goiás', value: 'GO'},
{label: 'Mato Grosso do Sul', value: 'MS'}, {label: 'Mato Grosso', value: 'MT'}, {label: 'Distrito Federal', value: 'DF'},
{label: 'Bahia', value: 'BA'}, {label: 'Sergipe', value: 'SE'}, {label: 'Alagoas', value: 'AL'},
{label: 'Paraíba', value: 'PB'}, {label: 'Piauí', value: 'PI'}, {label: 'Pernambuco', value: 'PE'},
{label: 'Rio Grande do Norte', value: 'RN'}, {label: 'Maranhão', value: 'MA'}, {label: 'Ceará', value: 'CE'}];

And in the HTML it looks like this:

<p-dropdown [options]="estados" [autoWidth]="false" placeholder="UF"></p-dropdown>

Again, could you please give a north? Thanks!

    
asked by anonymous 28.12.2018 / 01:18

1 answer

0

I do not understand your question, the data you want to sort in fixed alphabetical order, I do not understand why you do not order them in the states variable yet, however,

let estados = [
{label: 'Acre', value: 'AC'}, {label: 'Amazonas', value: 'AM'}, {label: 'Amapa', value: 'AP'},
{label: 'Pará', value: 'PA'}, {label: 'Roraima', value: 'RR'}, {label: 'Rondonia', value: 'RO'},
{label: 'Tocantins', value: 'TO'}, {label: 'Rio Grande do Sul', value: 'RS'}, {label: 'Santa Catarina', value: 'SC'},
{label: 'Paraná', value: 'PR'}, {label: 'São Paulo', value: 'SP'}, {label: 'Rio de Janeiro', value: 'RJ'},
{label: 'Minas Gerais', value: 'MG'}, {label: 'Espirito Santo', value: 'ES'}, {label: 'Goiás', value: 'GO'},
{label: 'Mato Grosso do Sul', value: 'MS'}, {label: 'Mato Grosso', value: 'MT'}, {label: 'Distrito Federal', value: 'DF'},
{label: 'Bahia', value: 'BA'}, {label: 'Sergipe', value: 'SE'}, {label: 'Alagoas', value: 'AL'},
{label: 'Paraíba', value: 'PB'}, {label: 'Piauí', value: 'PI'}, {label: 'Pernambuco', value: 'PE'},
{label: 'Rio Grande do Norte', value: 'RN'}, {label: 'Maranhão', value: 'MA'}, {label: 'Ceará', value: 'CE'}];
 
function comparar(a,b) {
  if(a.label < b.label) {
     return -1;
  }
  else if(a.label > b.label) {
    return 1;
  }
}

console.log(estados.sort(comparar));
    
28.12.2018 / 18:24