Doubt in Solution of Prblem "Form Biggest Number" FFrom challenge section of array

I have made logic which works on many inputc even with the given sample input. can u tell me what’s wrong in this code -
////////"
#include
#include
using namespace std;
int lastDigit(long i){
int r;
while(i!=0){
r=i%10;
i/=10;
}return r;
}
int main(){
int t;cin>>t;
while(t–){
int n;cin>>n;
long a[n];
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(lastDigit(a[i])<lastDigit(a[j])) swap(a[i],a[j]);
else if(lastDigit(a[i])==lastDigit(a[j]) && a[i]<a[j]) swap(a[i],a[j]);
}
}
for(int i=0;i<n;i++) cout<<a[i];
}
return 0;
}
"////////

hi ayush
please save your code on the coding blocks online ide

in my code -“https://ide.codingblocks.com/s/76258”.

i don’t get it where my logic is wrong , or there is some is some other logic PLEASE TELL !!!

You code is not producing correct results for input
8
1044 2522 2612 4987 2813 1223 4853 15
your result: 498748532813261225221223104415
correct result: 498748532813261225221512231044

In my previous Code-“https://ide.codingblocks.com/s/76534” i have corrected your recent test case but now also it is showing Wrong Answer

Now also i didn’t get it

can you plz add comments in your code so that i can understand your code well

Ya sure - “https://ide.codingblocks.com/s/76534

Please Help I am Stuck on this Problem ,Give me a hint!!!