Home > OS >  Sort one Array values based on another Array
Sort one Array values based on another Array

Time:02-03

I have two array Applications and ApplicationOrder with below data. I want to sort the data in Applications based on Order value from ApplicationOrder Array, Where order number are repeating then we need to sort on Title field from Applications.ID field is common in both sets of data.

Applications = 
[    {
        "ID": 30,
        "Title": "Balance",
        "Acronym": null,
        "Link": {
            "$2_1": "https:abc.com",
            "$1_1": "https:abc.com"
        }
    },
    {
        "ID": 12,
        "Title": "Scorecard",
        "Acronym": null,
        "Link": {
            "$2_1": "https:xyz.com",
            "$1_1": "https:xyz.com"
        }
    },
    {
        "ID": 62,
        "Title": "Best Practices",
        "Acronym": null,
        "Link": {
            "$2_1": "https:xyz.com",
            "$1_1": "https:xyz.com"
        }        
    },
    {
        "ID": 15,
        "Title": "User Actions",
        "Acronym": null,
        "Link": {
            "$2_1": "https:xyz.com",
            "$1_1": "https:xyz.com"
        },        
    }];

ApplicationOrder = [{"Id":"30","Order":"4"},{"Id":"12","Order":"4"},{"Id":"62","Order":"2"},{"Id":"15","Order":"1"}];

First sort based on the order:

User Actions - Order 1
Best Practices - Order 2
Scorecard - Order 4
Balance - Order 4

Sort second time based on title since two numbers have same order

User Actions - Order 1
Best Practices - Order 2
Balance - Order 4
Scorecard - Order 4

Output should be as follows:

Applications = 
[ {
        "ID": 15,
        "Title": "User Actions",
        "Acronym": null,
        "Link": {
            "$2_1": "https:xyz.com",
            "$1_1": "https:xyz.com"
        },
        
    },
    {
        "ID": 62,
        "Title": "Best Practices",
        "Acronym": null,
        "Link": {
            "$2_1": "https:xyz.com",
            "$1_1": "https:xyz.com"
        }        
    },
    {
        "ID": 30,
        "Title": "Balance",
        "Acronym": null,
        "Link": {
            "$2_1": "https:abc.com",
            "$1_1": "https:abc.com"
        }
    },
    {
        "ID": 12,
        "Title": "Scorecard",
        "Acronym": null,
        "Link": {
            "$2_1": "https:xyz.com",
            "$1_1": "https:xyz.com"
        }
    }];

Thank you in advance for your help.

CodePudding user response:

You could use reduce() and sort() methods to achieve this:

const Applications = [
  {
    ID: 30,
    Title: "Balance",
    Acronym: null,
    Link: {
      "$2_1": "https:abc.com",
      "$1_1": "https:abc.com"
    }
  },
  {
    ID: 12,
    Title: "Scorecard",
    Acronym: null,
    Link: {
      "$2_1": "https:xyz.com",
      "$1_1": "https:xyz.com"
    }
  },
  {
    ID: 62,
    Title: "Best Practices",
    Acronym: null,
    Link: {
      "$2_1": "https:xyz.com",
      "$1_1": "https:xyz.com"
    }        
  },
  {
    ID: 15,
    Title: "User Actions",
    Acronym: null,
    Link: {
      "$2_1": "https:xyz.com",
      "$1_1": "https:xyz.com"
    },        
  }
];

const ApplicationOrder = [
  {"Id":"30","Order":"4"},
  {"Id":"12","Order":"4"},
  {"Id":"62","Order":"2"},
  {"Id":"15","Order":"1"}
];


// Function to sort the data in Applications based on Order value from ApplicationOrder Array
const orderMap = ApplicationOrder.reduce((acc, item) => {
// Create a map of the order values keyed by ID
acc[item.Id] = item.Order;
return acc;
}, {});


Applications.sort((a, b) => {
// Get the order values for each application based on the map
const aOrder = orderMap[a.ID];
const bOrder = orderMap[b.ID];
if (aOrder !== bOrder) {
// If the order values are different, sort based on order value
return aOrder - bOrder;
} else {
// If the order values are the same, sort based on the Title field
return a.Title.localeCompare(b.Title);
}
});

console.log(Applications);

CodePudding user response:

You could get an object and sort woth value.

const
    applications = [{ ID: 30, Title: "Balance", Acronym: null, Link: { $2_1: "https:abc.com", $1_1: "https:abc.com" } }, { ID: 12, Title: "Scorecard", Acronym: null, Link: { $2_1: "https:xyz.com", $1_1: "https:xyz.com" } }, { ID: 62, Title: "Best Practices", Acronym: null, Link: { $2_1: "https:xyz.com", $1_1: "https:xyz.com" } }, { ID: 15, Title: "User Actions", Acronym: null, Link: { $2_1: "https:xyz.com", $1_1: "https:xyz.com" } }],
    applicationOrder = [{ Id: "30", Order: "4" }, { Id: "12", Order: "4" }, { Id: "62", Order: "2" }, { Id: "15", Order: "1" }],
    order = Object.fromEntries(applicationOrder.map(({ Id, Order }) => [Id, Order]));
    
applications.sort((a, b) =>
    order[a.ID] - order[b.ID] || 
    a.Title.localeCompare(b.Title)
);

console.log(applications);
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related