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

153
Views
sorting strings in an object inside an array

I've been trying to sort string inside an object which is inside an array. By splitting the string into array, I successfully sorted the array. I turn the array back to string afterwards. but later when I printed the result, the object inside the array was the same as before. Here is my code:

 function merge(arr, needed_length){

    for(var i = 0; i < arr.length; i++){
        console.log(arr[i]['A1'].split(', ').sort(function(a, b){
            return b - a;
        }).join(', '));

        console.log(arr[i]);
    }

}

console.log(merge([{A1:'8, 7, 9'}, {A1:'4, 8, 6'}, {A1:'2, 4, 3'}], 5));

and here is the printed result:

9, 8, 7
{ A1: '8, 7, 9' }
8, 6, 4
{ A1: '4, 8, 6' }
4, 3, 2
{ A1: '2, 4, 3' }

Can someone help me to understand why the object doesn't change? Thank you in advance :)

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

0

You need to assign the sorted string back to A1:

function merge(arr){
    for(let obj of arr){
        obj.A1 = obj.A1.split(', ').sort((a, b) => b - a).join(', ');
    }
    return arr;
}

let arr = [{A1:'8, 7, 9'}, {A1:'4, 8, 6'}, {A1:'2, 4, 3'}];
merge(arr);
console.log(arr);

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!