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

171
Views
Sort the elements before inserting them into the table

I'm having a problem sorting the elements in the array based on the variable number. Once sorted, they must be inserted into the table. I try to use sort on item but I get the error.

I get the following error:

item.sort is not a function

How can I solve?

let myArr = [{
    type : "Image", 
    id_a: '123456', 
    number: 2, 
    id: 2,
    },{
    type: 'Text', 
    id_a: '123456', 
    number: 4, 
    id: 4
    },{
    type: 'Testo', 
    id_a: '564312', 
    number: 1, 
    id: 1},{
    type: 'Title', 
    id_a: '123456', 
    number: 3, 
    id: 3
    }
    ]
    
var id_url=123456;
var results = {};
for (var i = 0, len = myArr.length; i < len; i++) {
    var id_art = myArr[i].id_a;
    if (id_art == id_url) {
        results[i] = myArr[i];
        }
        
}

console.log(results);

 
Object.entries(results).forEach(item => {
    item = item[1];
    console.log(item)
    item.sort(function (a, b) {
           if (a.number != b.number) {
               return (a.number - b.number);
  }
 });
    let child = document.createElement("tr");
                 child.innerHTML = `
                    <td>${item.number}</td>
                    <td>${item.type}</td>`;
                    document.querySelector('#my-table').appendChild(child);
 })
<table id="my-table" width="90%">
                    <tr>
                        <th>Number</th>
                        <th>Type</th>
                        
                    </tr>

                </table>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your sorting function is correct but sort is a built-in function for arrays taking a function as the parameter. You can use it like below:

let myArr = [{
    type : "Image", 
    id_a: '123456', 
    value: 'moto_guzzi_v100_mandello.jpg', 
    number: 2, 
    id: 2,
    },{
    type: 'Text', 
    id_a: '123456', 
    value: 'The star of the Piaggio group stand is without doubt ... Discover all his secrets with our video', 
    number: 4, 
    id: 4
    },{
    type: 'Testo', 
    id_a: '564312', 
    value: 'Let find out together with our video', 
    number: 1, 
    id: 1},{
    type: 'Title', 
    id_a: '123456', 
    value: 'Moto Guzzi V100 Mandello, the queen of EICMA 2021', 
    number: 3, 
    id: 3
    }
    ]
    
var id_url=123456;
var results = {};
for (var i = 0, len = myArr.length; i < len; i++) {
    var id_art = myArr[i].id_a;
    if (id_art == id_url) {
        results[i] = myArr[i];
        }
        
}

console.log(results);

 
console.log("Before Sort", myArr);
const sortedArr =  myArr.sort(function (a, b) {
           if (a.number != b.number) {
               return (a.number - b.number);
  }
 });
console.log("After Sort", sortedArr);

about 4 years ago · Juan Pablo Isaza Report

0

Try the following.

It's easier to sort.

      const filterList = myArr.filter((now) => now.id_a == id_url);
      const sortList = filterList.sort((a, b) => a.number - b.number);
      const results = { ...sortList };

      for (const item of sortList) {
        let child = document.createElement("tr");
        child.innerHTML = `
                    <td>${item.number}</td>
                    <td>${item.type}</td>`;
        document.querySelector("#my-table").appendChild(child);
      }
about 4 years ago · Juan Pablo Isaza 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!