MsNichols
MsNichols

Reputation: 1133

Sort Array Objects by Key Based on Another Array

I have an array called "sortOrder" in which i expect to use as the model order i want to sort my "dataSet" array with.

const sortOrder = [
           "FirstName",
           "LastName",
           "Address", 
           "City", 
           "State", 
           "Zip", 
           "Country" ]

const dataSet = [
         { Zip: "00199", 
           City: "Birmingham",
           State: "AL", 
           LastName: "Ellis", 
           Country: "USA",
           FirstName: "Dominique",
           Address: "127 East Avenue J St",
         },
         { Zip: "95421", 
           City: "San Francisco",
           State: "CA", 
           LastName: "Orlando", 
           Country: "USA",
           FirstName: "Mitchell",
           Address: "90 Market Street Unit 2",
         },
         { Zip: "DN11 5XQ", 
           City: "Wadworth",
           State: "AL", 
           LastName: "Connolly", 
           Country: "UK",
           FirstName: "John",
           Address: "130  New Dover Rd",
          }
         ]

Goal: To have something like the below object ordered based on keys found in the "sortOrder" array:

     { FirstName: "Dominique",
       LastName: "Ellis",
       Address: "127 East Avenue J St",
       City: "Birmingham",
       State: "AL",
       Zip: "00199",
       Country: "USA" }

I've tried to use sort method to handle the sort but its not working. Any advice on how to get this dataset sorted by the key?

My failed attempt:

    dataSet.sort((a, b) => {
         return sortOrder.indexOf(a) - sortOrder.indexOf(b);
    });

    console.log("SortedDataSet", dataSet);

Upvotes: 0

Views: 181

Answers (2)

YATIN GUPTA
YATIN GUPTA

Reputation: 955

const dataSet = [
         { Zip: "00199", 
           City: "Birmingham",
           State: "AL", 
           LastName: "Ellis", 
           Country: "USA",
           FirstName: "Dominique",
           Address: "127 East Avenue J St",
         },
         { Zip: "95421", 
           City: "San Francisco",
           State: "CA", 
           LastName: "Orlando", 
           Country: "USA",
           FirstName: "Mitchell",
           Address: "90 Market Street Unit 2",
         },
         { Zip: "DN11 5XQ", 
           City: "Wadworth",
           State: "AL", 
           LastName: "Connolly", 
           Country: "UK",
           FirstName: "John",
           Address: "130  New Dover Rd",
          }
         ];

const sortOrder = [
           "FirstName",
           "LastName",
           "Address", 
           "City", 
           "State", 
           "Zip", 
           "Country" ];

let sortedDataSet = [];
dataSet.forEach((d) => {
    let entry = {};
    sortOrder.forEach((s) => {
        entry[s] = d[s];
    });
    sortedDataSet.push(entry);
});

console.log('Original Dataset');
console.log(dataSet);
console.log('Sorted Dataset');
console.log(sortedDataSet);

Upvotes: 1

Ketan Ramteke
Ketan Ramteke

Reputation: 10655

const sortOrder = [
  "FirstName",
  "LastName",
  "Address",
  "City",
  "State",
  "Zip",
  "Country"
]

let dataSet = [{
Zip: "00199",
City: "Birmingham",
State: "AL",
LastName: "Ellis",
Country: "USA",
FirstName: "Dominique",
Address: "127 East Avenue J St",
  },
  {
Zip: "95421",
City: "San Francisco",
State: "CA",
LastName: "Orlando",
Country: "USA",
FirstName: "Mitchell",
Address: "90 Market Street Unit 2",
  },
  {
Zip: "DN11 5XQ",
City: "Wadworth",
State: "AL",
LastName: "Connolly",
Country: "UK",
FirstName: "John",
Address: "130  New Dover Rd",
  }
]

let solution = dataSet.map(data => {
  let obj = {}
  for (let key of sortOrder) {
obj[key] = data[key];
  }
  return obj
})

console.log(solution)

Upvotes: 1

Related Questions