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

122
Views
Does sort alter my json array or does it return a sorted array

I am trying to sort a json array on my Table header click. I am trying to follow this web article. My function runs but the array is never changed. Here is the function:

function setInvoiceSortField(field) {
    console.log(field);
    let sortedInvoices = this.state.invoiceList;
    if (field !== null) {
        sortedInvoices.sort((a, b) => {
            console.log(`a ${a[field]}`);
            console.log(`b ${b[field]}`);
            if (a[field] < b[field]) {
                return -1;
            }
            if (a[field] > b[field]) {
                return 1;
            }
            return 0;
        });
        this.setState({ invoiceList: sortedInvoices });
    }
}

I can post an example of the json array and/or the entire .js file if needed. Is .sort() changing sortedInvoices or does it return a sorted array? Any hints or help are extremely appreciated!

UPDATE

I added an if to check to see if they match post sort and they do.

function setInvoiceSortField(field) {
    console.log(field);
    let sortedInvoices = this.state.invoiceList;
    if (field !== null) {
        sortedInvoices.sort((a, b) => {
            console.log(`a ${a[field]}`);
            console.log(`b ${b[field]}`);
            if (a[field] < b[field]) {
                return -1;
            }
            if (a[field] > b[field]) {
                return 1;
            }
            return 0;
        });
        if (sortedInvoices === this.state.invoiceList) {
            console.log("matched")
        }
        this.setState({ invoiceList: sortedInvoices });
    }
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

From the MDN website:

The sort() method sorts the elements of an array in place, and returns the sorted array. Example copied.

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]
about 4 years ago · Juan Pablo Isaza Report

0

Since you are directly assigning the array(this.state.invoiceList) to sortedInvoices it will be done by reference. So even though your array(sortedInvoices) is different from invoiceList, both the references are same. Hence changes are not reflected.

function setInvoiceSortField(field) {
console.log(field);
let sortedInvoices = [...this.state.invoiceList];
if (field !== null) {
    sortedInvoices.sort((a, b) => {
        console.log(`a ${a[field]}`);
        console.log(`b ${b[field]}`);
        if (a[field] < b[field]) {
            return -1;
        }
        if (a[field] > b[field]) {
            return 1;
        }
        return 0;
    });
    this.setState({ invoiceList: sortedInvoices });
  }
}
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!