Kevin Kevin - 18 days ago 4x
HTML Question

Sorting selectbox options from database

I'm trying to sort my selectbox options in alphabetically ascending order. (Starting at A, going all the way to Z)

I tried using a sorter I found in another question but I can't seem to get it to work on all of my selectboxes. I tried calling the selectboxes by using document.getElementsByTagName() but that doesn't seem to work either.

This is the function I'm trying to use:

function sortlist() {

var selectOptions = document.getElementsByClassName("sortList");
var selectOptionsTexts = new Array();

for (i = 2; i < selectOptions.length; i++) {
selectOptionsTexts[i - 2] =
selectOptions.options[i].text.toUpperCase() + "," +
selectOptions.options[i].text + "," +
selectOptions.options[i].value + "," +


for (i = 2; i < selectOptions.length; i++) {
var parts = selectOptionsTexts[i - 2].split(',');

selectOptions.options[i].text = parts[1];
selectOptions.options[i].value = parts[2];
if (parts[3] == "true") {
selectOptions.options[i].selected = true;
} else {
selectOptions.options[i].selected = false;

Thanks in advance


Since you're using laravel and getting your data from a database as in the title, you can just sort it alphabetically once you select it from the database. you can use the Laravel Query Builder for that or the default sql methode.

Laravel Query:

DB::table('your_table')->orderBy('col_name', 'asc'); //asc = ascending, desc = descending

SQL Query:

SELECT * FROM your_table ORDER BY column_name ASC //ASC = ascending, DESC = descending

For more things about the laravel query builder you can visit the laravel documentation at:

I hope this will solve your problems.