Home > Software engineering >  looking to sort items in object to match order of array
looking to sort items in object to match order of array

Time:07-30

I'm trying to reorder the items in an object so they will match the order of an array. The object is the row data and the array is the column names.

Here are the order of column names I want to match:

columnNames    [
    "System.Id",
    "System.WorkItemType",
    "System.Title",
    "System.AssignedTo",
    "System.State",
    "Microsoft.VSTS.Common.ActivatedDate",
    "System.ChangedDate",
    "Microsoft.VSTS.Common.ClosedDate"
    ]

Here is an example of the object items I'm trying to reorder:

 fields = {"System.Id":7993,"System.WorkItemType":"Task","System.State":"Closed","System.AssignedTo":"Jack Smith","System.ChangedDate":"2022-07-19T12:14:25.193Z","System.Title":"Update Dev Environment","Microsoft.VSTS.Common.ActivatedDate":"2022-07-19T12:13:49.713Z","Microsoft.VSTS.Common.ClosedDate":"2022-07-19T12:14:25.193Z"}

I want the object to be ordered like this:

{
"System.Id":7993,
"System.WorkItemType":"Task",
"System.Title":"Update Dev Environment",
"System.AssignedTo":"Jack Smith",
"System.State":"Closed",
"Microsoft.VSTS.Common.ActivatedDate":"2022-07-19T12:13:49.713Z",
"System.ChangedDate":"2022-07-19T12:14:25.193Z",
"Microsoft.VSTS.Common.ClosedDate":"2022-07-19T12:14:25.193Z"
}

I tried converting the fields object into an array and use the sort function like this:

Object.entries(fields).forEach(([key, value]) => { sortingfields.push(fields[key]) });

sortingfields.sort((a, b) => columnNames.indexOf(a) - columnNames.indexOf(b))

The sort function doesn't do anything. The order doesn't change. From what I've read it seems the sort function is what I should use but I can't figure out where I've gone wrong.

CodePudding user response:

you can use map for that

you just map over the keys you want and get it from the object like this

const orderData = (data, order) => order.map(k => data[k]) 



const columnNames   =  [
    "System.Id",
    "System.WorkItemType",
    "System.Title",
    "System.AssignedTo",
    "System.State",
    "Microsoft.VSTS.Common.ActivatedDate",
    "System.ChangedDate",
    "Microsoft.VSTS.Common.ClosedDate"
    ]

const single = {"System.Id":7993,"System.WorkItemType":"Task","System.State":"Closed","System.AssignedTo":"Jack Smith","System.ChangedDate":"2022-07-19T12:14:25.193Z","System.Title":"Update Dev Environment","Microsoft.VSTS.Common.ActivatedDate":"2022-07-19T12:13:49.713Z","Microsoft.VSTS.Common.ClosedDate":"2022-07-19T12:14:25.193Z"}
    
const data = [{"System.Id":7993,"System.WorkItemType":"Task","System.State":"Closed","System.AssignedTo":"Jack Smith","System.ChangedDate":"2022-07-19T12:14:25.193Z","System.Title":"Update Dev Environment","Microsoft.VSTS.Common.ActivatedDate":"2022-07-19T12:13:49.713Z","Microsoft.VSTS.Common.ClosedDate":"2022-07-19T12:14:25.193Z"}, {"System.Id":7993,"System.WorkItemType":"Task","System.State":"Closed","System.AssignedTo":"Jack Smith","System.ChangedDate":"2022-07-19T12:14:25.193Z","System.Title":"Update Dev Environment","Microsoft.VSTS.Common.ActivatedDate":"2022-07-19T12:13:49.713Z","Microsoft.VSTS.Common.ClosedDate":"2022-07-19T12:14:25.193Z"}]


console.log(orderData(single, columnNames))

console.log(data.map(d => orderData(d, columnNames)))

CodePudding user response:

I'm not really sure of what you want to achieve but here is my answer:

const fieldEntries = Object.entries(fields);
const entriesResult = fieldEntries.sort((entryA, entryB) => {
  const [keyA] = entryA;
  const [keyB] = entryB;
  return columnNames.indexOf(keyA) - columnNames.indexOf(keyB)
})
const objResult = Object.fromEntries(entriesResult)

This gives the following value for objResult:

{
System.Id:7993
System.WorkItemType:"Task"
System.Title:"Update Dev Environment"
System.AssignedTo:"Jack Smith"
System.State:"Closed"
Microsoft.VSTS.Common.ActivatedDate:"2022-07-19T12:13:49.713Z"
System.ChangedDate:"2022-07-19T12:14:25.193Z"
Microsoft.VSTS.Common.ClosedDate:"2022-07-19T12:14:25.193Z"
}

Sandbox: https://playcode.io/932608

  • Related