How to sort Key value pair array

sainath3
Mega Guru

Hi Team,

 

Table 1:

 

NameID
Bachu Sainath101

 

Table 2:

IDAmountCurrencyBranch
10120USDDelhi
10180GBPPune
10130EURBangalore
10110USDHyd

Requirement:

1.Amount should convert to EUR, then, display Branch with Amount in Descending order (Amount):

USD to EUR: 1.09

GBP to EUR: 0.85

 

Expected Output in Table 1:

 

NameIDBranch1Amount1Branch2Amount2Branch3Amount3Branch4Amount4
Bachu Sainath101Bangalore70Pune68Delhi22Hyd11

 

My code:

var branch,amount;

var amountForbranch={ };

var gr=new GlideRecord('table1');

gr.addQuery('ID','101');

gr.query();

if(gr.next()){

var gr1=new GlideRecord('table2');

gr1.addQuery('ID',gr.id);

gr1.query();

while(gr1.next()){

var lineamount = covert_currency(gr1.u_currency, 'EUR', gr1.amount);//Convert Amount to EUR

amount=lineamount.toString();

branch=gr1.branch.toString();

amount=gr1.lineamount.toString();

amountForbranch[branch]=amount;

}

for(var key in amountForbranch){
gs.info(" key is " + key + " amountForbranch[key] " + amountForbranch[key]);

}

}

 

function covert_currency(from_curr, to_curr, amount) {
var converted_amount;
if (from_curr == 'EUR') {
return amount;
}
var CurrConv = new GlideRecord('fx_rate');
CurrConv.addQuery('currency.code', to_curr);
CurrConv.orderByDesc('sys_created_on');
CurrConv.query();
while (CurrConv.next()) {
converted_amount = CurrConv.u_conversion_rate * amount;
}
return converted_amount.toFixed(2);
}

Actual Output is:

key is Delhi amountForbranch[key] 22
key is Pune amountForbranch[key] 68
key is Bangalore amountForbranch[key] 70
key is Hyd amountForbranch[key] 11

 expected output is:

 key is Bangalore amountForbranch[key] 70
key is Pune amountForbranch[key] 68
key is Delhi amountForbranch[key] 22
key is Hyd amountForbranch[key] 11

Please help me how to sort Amount(After converting to EUR) & Branch in Descending order.

6 REPLIES 6

Azem
Tera Guru

Hi @sainath3 ,

 

Try the script below & let me know your luck.

 

 

 

var arr = [];
for(var key in amountForbranch){
arr.push([key, amountForbranch[key]);
}
arr.sort(function(a,b){
    return b[1] - a[1];
});

var objSorted = {};
arr.forEach(function(item){
    objSorted[item[0]]=item[1]
})

for(var key in objSorted){
gs.info(" key is " + key + " amountForbranch[key] " + objSorted[key]);
}

 

 

 

Kind Regards,

Hi Azem,

Its not working.

SwarnadeepNandy
Mega Sage

Hello @sainath3,

 

To sort the amount and branch in descending order, you can use the Object.entries method to convert the amountForbranch object into an array of key-value pairs, and then use the Array.sort method to sort the array by the value. You can also use the Array.reverse method to reverse the order of the array.

Here is an example of how you can modify your code to achieve this:

// Convert the amountForbranch object into an array of key-value pairs
var amountForbranchArray = Object.entries(amountForbranch);

// Sort the array by the value in ascending order
amountForbranchArray.sort(function(a, b) {
  return a[1] - b[1];
});

// Reverse the order of the array to get descending order
amountForbranchArray.reverse();

// Loop through the array and print the key and value
for (var i = 0; i < amountForbranchArray.length; i++) {
  var key = amountForbranchArray[i][0];
  var value = amountForbranchArray[i][1];
  gs.info("key is " + key + " amountForbranch[key] " + value);
}

 

The expected output of the code would be

key is Bangalore amountForbranch[key] 70
key is Pune amountForbranch[key] 68
key is Delhi amountForbranch[key] 22
key is Hyd amountForbranch[key] 11

 

Hope this helps.

 

Kind Regards,

Swarnadeep Nandy

Hi Swarnadeep,

 

It's not working.