-1

I have this array :

displayedColumns = ['CompanyName','Ticker', 'Id', 'Name', 'ActualTradeDate', 
  'Spot', 'PreviousTradeDate', 'PreviousSpot', 'FPrice', 'Status']

and an array of objects with the above values as properties :

data : [{Ticker : ".. " ......... Status : "... " } , .... {...} ] 

but the properties in the data array are not sorted in the 'displayedColums' order (as wanted )

the end wanted result is :

data = [{  CompanyName : ".. " Ticker : ".."......Status : ".." },
{  CompanyName : ".. " Ticker : ".."......Status : ".." },....
{  CompanyName : ".. " Ticker : ".."......Status : ".." }]

**just to be clear, I am not looking to sort the array by asc/desc values of a certain property. I want to change the order of the properties in the array to 'displayedColumn' order

I am not sure how to sort an array by property order. thanks a lot

Jonathan
  • 99
  • 1
  • 8
  • 2
    Please share some sample input and expected output. – Hassan Imam May 01 '18 at 11:24
  • As far as I can tell from your question, you want to sort an array of objects by an object property. That question has been repeatedly asked and answered, so I've closed your question as a duplicate of those. If that's **not** what you're asking, please update the question with a clear statement of what you want to do, along with sample input and output, and we can reopen the question. – T.J. Crowder May 01 '18 at 11:28
  • added a comment – Jonathan May 01 '18 at 11:31
  • the properties of an object has not order (you neend't anyway) If you want to order by property, google is our friend – Eliseo May 01 '18 at 11:32

1 Answers1

1

Objects are never sorted, but arrays are. Therefore you could turn your array of objects into an array of arrays, containing the objects values in displayedColumns order:

 const result = data.map(obj => displayedColumns.map(key => obj[key]));
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151