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

140
Views
Reactjs: how to sort columns if column name is dynamic

I know that to sort a column we use sorter: (a, b) => a.monthName - b.monthName but in my case, my column name (i.e. monthName) is not hardcoded but is generated dynamically as I load the component. So for this my row object looks like:

   November: xyz,
   December: xyz,
   January: xyz,
   forecasted: xyz

I'm adding Nov, Dec, Jan i.e. the names of the months dynamically in the state via

const newColumn = {
        title: monthName,
        dataIndex: monthName,
        key: monthName,
        sorter: (a, b) => a.monthName - b.monthName,
        render(text,record){}
       }

I can't hardcode the month names as I need last 3 months' names. Is there a way I can pass column name to this sort function? How can I sort the columns in this case?

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

0

You could save the column's name into a variable (or in this case state) and address the variable when sorting.

Example:

var columnName = 'monthName';

(a, b) => a[columnName] - b[columnName];
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!