Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
Materialize CSS Autocomplete sorting based on a specific word

I'm using Materialize CSS Autocomplete for my Google Apps Script with the data populated form my Google Contacts. I would like to be able to sort the order in which the contacts appear based on a specific word in their names. For example, lets say I have the following contacts:

  • Jason Garcia
  • Jonathan Jackson Mobile
  • Monica Stokes
  • Amy Wang Mobile
  • Aaron Bridges

I would like the contacts which contain the word Mobile to appear on top of the list and the rest in the alphabetical order:

  • Amy Wang Mobile
  • Jonathan Jackson Mobile
  • Aaron Bridges
  • Jason Garcia
  • Monica Stokes

To do that, I've been trying to use the sortFunction from the Materialize CSS documentation but with no success. How could I achieve something like that?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can sort the array by checking if it contains the term "mobile", see below. This returns first all which do contain "mobile" and then all others.

const arr = ["Tom Mobile", "Sam", "Jess Mobile"]
arr.sort( (el1) => {
   if(el1.search(/mobile/gi) == -1) return 1
   return -1
}))

But that is for array and you do not need arrays, therefore that answer was general and not specific. Therefore I did a quick demo of what you actually wanted:

  document.addEventListener('DOMContentLoaded', function() {
    const options = {
        data : {
        "Tom mobile" : null,
        "Thomas" : null,
        "Tim mobile" : null
      },
      sortFunction : sortBy
    }
    var elems = document.querySelectorAll('.autocomplete');
    var instances = M.Autocomplete.init(elems, options);
  });
  
  // Sort function for sorting autocomplete results
  // prioritize if text contains mobile
  function sortBy(a, b) {    
    return  b.indexOf("mobile") - a.indexOf("mobile");
  }

Reference

  • array.sort()
  • string.search()
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda