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

122
Vistas
How to sort particular array position based on the Id in Vuejs?
<div v-for="(item, index) in gr" 
:key="space.id"
class="val-name">
</div>

I have done that logic using the below few line changes in the api call, like

with that, what i am trying to achieve finally i got it. But issue is, i cant expect the array value same all the time. So i have decided to re-arrange the array values based on the ID.

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

0

orderMap would be your desired index outcome.

array would be your API response.

map orderMap with a function that finds the desired index and places it in our sorted array.

const orderMap = ['third', 'second', 'first'];

const array = [{name: 'first'}, {name: 'second'}, {name: 'third'}];

let sorted = orderMap.map(function(sortBy){
  return this.find(function(toSort){return toSort.name === sortBy})
}, array)

console.log(sorted);

// as method -> Result is your response Data. OrderMap the map to order by. 
sortResultSet(result, orderMap) {
  return orderMap.map(function(sortBy){
    return this.find(function(toSort){return toSort.name === sortBy})
  }, result)
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could write a custom sorting function so it sorts them according to your needs. For eg:

function Sort(item1, item2){
  if (item1.name > item2.name)
    return 1;
  else if (item1.name < item2.name)
    return -1;
  else 
    return 0;
}

And then call it on the array

array.sort(Sort);
console.log(array);

If you want to you can omit the function declaration and just pass function as a parameter.

array.sort(function (item1, item2) {
  <same code from above>
});

And then just add it to the desired object from Vue

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