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

95
Vistas
Knockout js foreach binding alphabetically

I'm totally new in knockout.

Good you help me to understand how I need to change this code:

<ol class="items" data-bind="foreach: getDisplayedItems()">
    <li class="item">
        <a class="simple" data-bind="html: label, attr: {href:url}, visible: count >= 1, css: { active: is_selected }"></a>
        <span class="count" data-bind="text: count"></span>
    </li>
</ol>

to make it in alphabetically order?

Thanks for any tips Regards

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

0

You could sort them in-place:

<ol class="items" data-bind="foreach: getDisplayedItems().sort((a, b) => ko.unwrap(a.label) < ko.unwrap(b.label) ? -1 : 1)">
    <li class="item">
        <a class="simple" data-bind="html: label, attr: {href:url}, visible: count >= 1, css: { active: is_selected }"></a>
        <span class="count" data-bind="text: count"></span>
    </li>
</ol>

but I think that's ugly, and it's not re-usable. It's better to make a new observable array that contains the items in the right order:

function App {
    // the observable you already have
    this.getDisplayedItems = ko.observableArray([...]);

    // a computed observable based on that
    this.getDisplayedItemsAlphabetical = ko.pureComputed(() => this.getDisplayedItems.sorted((a, b) => ko.unwrap(a.label) < ko.unwrap(b.label) ? -1 : 1));
}

and

<ol class="items" data-bind="foreach: getDisplayedItemsAlphabetical">
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