How to leave a field to select names in alphabetical order

Asked

Viewed 103 times

0

I wonder if you have any function of typescript or javascript that performs this organization in alphabetical order. if yes which and how to use it

1 answer

1

If you want to create a checkbox with names in alphabetical order, you can use an array. You can sort an array in alphabetical order using the Sort method. Here is an example:

var vetor = Array ('Azul', 'Amarelo', 'Vermelho', 'Preto');
var vetor_ordenado = vetor.sort();

// Para criar um campo com este array, você faria isto:
var dropdown = document.getElementById("meu_campo");

// Fazendo loop no array
for (var i = 0; i < vetor_ordenado.length; ++i) {
    //Adicionando novos itens
    dropdown[dropdown.length] = new Option(vetor_ordenado[i], vetor_ordenado[i]);
}
<form id="myForm">
  <select id="meu_campo">
    <option>Selecione...</option>
  </select>
</form>

  • Thanks for the tip, I already have a field and I just need to alphabetize it.

  • @mateussantana Wow, you are just popular the field using the technique that the colleague taught you. If your field is static, then manually sort and fly! PS: If you want something more specific, consider editing your question and adding more details. Hugging!

Browser other questions tagged

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