Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

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)
}
over 4 years ago · Santiago Trujillo Report

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!