0

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);
2
  • Do you really need to use a sort function? I guess that you could accomplish what you want with a map. Commented Dec 9, 2020 at 4:22
  • Hmmm... let me rethink it. @danibrum Commented Dec 9, 2020 at 4:58

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

not the desired result. This doesnt sort.
can you give the original input with its corresponding sorted output.
Its what i posted in my question - i even gave an example of the sample output/goal @Ketan Ramteke - i want the dataSet array sorted in the same order as the sortOrder Array.
i. shoouldve known map was the direction to go. Thank you, this worked!
1
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.