I have a use case wherein I want to sort the below given array
const account = [
{
currency: "CNY",
available_balance: 1000
},
{
currency: "HKD",
available_balance: 100
},
{
currency: "CNY",
available_balance: 200
},
{
currency: "HKD",
available_balance: 1500
}];
Now I have to sort it as follows,
const output = [
{
currency: "CNY",
available_balance: 1000
},
{
currency: "CNY",
available_balance: 200
},
{
currency: "HKD",
available_balance: 1500
},
{
currency: "HKD",
available_balance: 100
}];
The one with higher balance in CNY is first followed by the lower balances of CNY. After that I want to display HKD with higher balance first.
I was able to sort according to the available_balance but with not currency.
sortedAccount = account.sort((a, b) => {
return b.available_balance - a.available_balance;
});
console.log(sortedAccount);
Please help 🙂
Thank you.