0

I have an object array

item_array = [{
                "Email Address": "c",
                "First Name": "a",
                "Last Name": "b",
                "Permission": "Training Administrator",
                "Telephone": "d",
                "User Group": "Company Administrator"
            },
            {
                "Email Address": "3",
                "First Name": "1",
                "Last Name": "2",
                "Permission": "6",
                "Telephone": "4",
                "User Group": "5"
            }];

How can i sort this array by given array like this

item_order = ["First Name", "Last Name", "Email Address", "Permission", "Telephone", "User Group"];

I need to sort object array of object like given array

iteam_order

Expected result

item_array = [{
                "First Name": "a",
                "Last Name": "b",
                "Email Address": "c",
                "Permission": "Training Administrator",
                "Telephone": "d",
                "User Group": "Company Administrator"
            },
            {
                "First Name": "1",
                "Last Name": "2",
                "Email Address": "3",
                "Permission": "6",
                "Telephone": "4",
                "User Group": "5"
            }];
adiga
  • 34,372
  • 9
  • 61
  • 83
  • No i need to sort this object array by given custom array not an alphabetic order –  Jan 17 '19 at 06:17
  • 1
    In JavaScript, sorting is for arrays, not for objects. See the linked post for detailed answer and possible alternatives. – Mohammad Usman Jan 17 '19 at 06:22

2 Answers2

6

You can use map and reduce like this:

const item_array = [{"Email Address":"c","First Name":"a","Last Name":"b","Permission":"Training Administrator","Telephone":"d","User Group":"Company Administrator"},{"Email Address":"3","First Name":"1","Last Name":"2","Permission":"6","Telephone":"4","User Group":"5"}],
      item_order = ["First Name","Last Name","Email Address","Permission","Telephone","User Group"];

const ordered = item_array.map(item =>
    item_order.reduce((acc, key) => ( acc[key] = item[key], acc ), {})
);

console.log(ordered)
adiga
  • 34,372
  • 9
  • 61
  • 83
0

Using the reference array indices as the sorting order, implement the sort logic in the sort function and extract the keys of the object and sort them according to the given array and push the sorted object inside the original array.

var item_array = [{ "Email Address": "c", "First Name": "a", "Last Name": "b", "Permission": "Training Administrator",  "Telephone": "d", "User Group": "Company Administrator" }, {"Email Address": "3", "First Name": "1", "Last Name": "2", "Permission": "6", "Telephone": "4", "User Group": "5"}];
var item_order = ["First Name", "Last Name", "Email Address", "Permission", "Telephone", "User Group"];
item_array.forEach((obj, idx, arr) => {
           arr[idx] = Object.keys(obj)
                            .sort((a, b) => {
                             return item_order.indexOf(a) - item_order.indexOf(b);
                             })
                            .reduce((acc, ele)=>{acc[ele] = obj[ele]; return acc;},{});
                  });

console.log(item_array);
      
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44