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

197
Views
Why my sorting array script not working in Vue.js?

Well, I have a problem with sorting this table by Date. The sort function as a determinant of what sort we should use - works, but the sort function doesn't work anymore and I don't know why.

html:

<th @click = "sort('data_produktu')" class="date">Data</th>

data:

messages: [],
   currentSort:'data_produktu',
     currentSortDir:'asc',

methods:

sort: function(s){
        
      if(s === this.currentSort) {
        this.currentSortDir = this.currentSortDir==='asc'?'desc':'asc';
      }
      this.currentSort = s;
      console.log(this.messages);
    }

computed:

sortedMessages:function() {
      return this.messages.sort((a,b) => {
        let modifier = 1;
        if(this.currentSortDir === 'desc') modifier = -1;
        if(a[this.currentSort] < b[this.currentSort]) return -1 * modifier;
        if(a[this.currentSort] > b[this.currentSort]) return 1 * modifier;
        return 0;
      });
    }

Array(console.log output because Im fetching it from DB):

0: {id_komunikat: '4', data_produktu: '2022-08-12 20:05:30', tresc: 'Example info', typ: '2', status_komunikatu: '1', …}
1: {id_komunikat: '5', data_produktu: '2022-08-12 20:05:36', tresc: 'Example info', typ: '2', status_komunikatu: '1', …}
2: {id_komunikat: '6', data_produktu: '2022-08-12 20:05:39', tresc: 'Example info', typ: '2', status_komunikatu: '1', …}
3: {id_komunikat: '7', data_produktu: '2022-08-12 20:05:47', tresc: 'Example info', typ: '1', status_komunikatu: '1', …}
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I see that data_produktu is a date, sorting date can't be sorted, with bigger than > or less than <, but by subtraction timestamps.

Ex:

array.sort(function(a,b){
  return new Date(b) - new Date(a);
});
about 4 years ago · Santiago Trujillo Report

0

The computed function looks suspicious to me. It probably would cause infinite loop, because you also mutate this.messages in-place by invoking .sort() method. I suggest change it to:

sortedMessages:function() {
  //       use `.slice()` to clone first
  this.messages.slice().sort((a, b) => ...)
about 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!