Coding A-Z Titles

I have codes 19 titles and wanted to display them alphabetically on a dropdown bar. I cannot seem to figure it out and have it so that it does stays in the a-z order.

This is the coding log I used

import wixData from “wix-data”;

$w.onReady(() => {
loadContinents();
});

let lastFilterTitle;
let lastFilterContinent;
let debounceTimer;
export function iTitle_keyPress(event, $w) {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
filter($w(‘#iTitle’).value, lastFilterContinent);
}, 500);
}

export function iContinent_change(event, $w) {
filter(lastFilterTitle, $w(‘#iContinent’).value);
}

function filter(title, continent) {
if (lastFilterTitle !== title || lastFilterContinent !== continent) {
let newFilter = wixData.filter();
if (title)
newFilter = newFilter.contains(‘articleTitle’, title);
if (continent)
newFilter = newFilter.contains(‘continent’, continent);
$w(‘#dataset1’).setFilter(newFilter);
lastFilterTitle = title;
lastFilterContinent = continent;
}
}

function loadContinents() {
wixData.query(‘Continents’)
.find()
.then(res => {
let options = [{“value”: ‘’, “label”: ‘Mostar todas as areas de atuacao ‘}];
options.push(…res.items.map(continent => {
return {“value”: continent.title, “label”: continent.title};
}));
$w(’#iContinent’).options = options;
});

}

Hi Gabriel,

Try using Array.sort() method. In your case, it should look similar to this (I copied the sort comparator from MDN docs):

options.sort(function(a, b) {
  var nameA = a.label.toUpperCase(); // ignore upper and lowercase
  var nameB = b.label.toUpperCase(); // ignore upper and lowercase
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  // names must be equal
  return 0;
});
$w('#iContinent').options = options;