How to sort Key value pair array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2023 08:02 AM
Hi Team,
Table 1:
Name | ID |
Bachu Sainath | 101 |
Table 2:
ID | Amount | Currency | Branch |
101 | 20 | USD | Delhi |
101 | 80 | GBP | Pune |
101 | 30 | EUR | Bangalore |
101 | 10 | USD | Hyd |
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:
Name | ID | Branch1 | Amount1 | Branch2 | Amount2 | Branch3 | Amount3 | Branch4 | Amount4 |
Bachu Sainath | 101 | Bangalore | 70 | Pune | 68 | Delhi | 22 | Hyd | 11 |
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2023 11:34 AM - edited 08-16-2023 11:40 AM
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2023 01:35 PM
Hi Azem,
Its not working.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2023 01:02 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2023 01:36 PM
Hi Swarnadeep,
It's not working.