How to sort Key value pair array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2023 02:10 PM
Hi Team,
Table 1:
Name | ID |
Bachu Sainath | 101 |
Table 2:
ID | Amount | Branch |
101 | 20 | Delhi |
101 | 80 | Pune |
101 | 30 | Bangalore |
101 | 10 | Hyd |
Requirement is to Print Branch with Amount in Descending order (Amount):
Expected Output in Table 1:
Name | ID | Branch1 | Amount1 | Branch2 | Amount2 | Branch3 | Amount3 | Branch4 | Amount4 |
Bachu Sainath | 101 | Pune | 80 | Bangalore | 30 | Delhi | 20 | Hyd | 10 |
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()){
branch=gr1.branch.toString();
amount=gr1.amount.toString();
amountForbranch[branch]=amount;
}
for(var key in amountForbranch){
gs.info(" key is " + key + " amountForbranch[key] " + amountForbranch[key]);
}
}
Actual Output is:
key is Delhi amountForbranch[key] 20
key is Pune amountForbranch[key] 80
key is Bangalore amountForbranch[key] 30
key is Hyd amountForbranch[key] 10
expected output is:
key is Pune amountForbranch[key] 80
key is Bangalore amountForbranch[key] 30
key is Delhi amountForbranch[key] 20
key is Hyd amountForbranch[key] 10
Please help me how to sort Amount & Branch in Descending order.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2023 05:19 PM
If the values are stored on the table already, an orderByDesc("amount") would do it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2023 09:35 PM
Sorry, I forgot to mention one more step of requirement.
In Table 2
var lineamount = covert_currency(gr1.u_currency, 'EUR', gr1.amount);//Convert Amount to EUR
amount=lineamount.toString();
Need to sort the amount after the Conversion
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2023 05:33 PM
Hi @sainath3 ,
You can use the orderByDesc() method in your code to sort it by amount.
Please refer to the code below:
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.orderByDesc('amount');
gr1.query();
while(gr1.next()){
branch=gr1.branch.toString();
amount=gr1.amount.toString();
amountForbranch[branch]=amount;
}
for(var key in amountForbranch){
gs.info(" key is " + key + " amountForbranch[key] " + amountForbranch[key]);
}
}
If my answer has helped with your question, please mark it as correct and helpful
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2023 09:35 PM
Hi Karan,
Sorry, I forgot to mention one more step of requirement.
In Table 2
var lineamount = covert_currency(gr1.u_currency, 'EUR', gr1.amount);//Convert Amount to EUR
amount=lineamount.toString();
Need to sort the amount after the Conversion