Facing error in code

#include
#include
#include<string.h>
using namespace std;

int main()
{
int i,a[100],n,j,no;
string s[100],sum;
cin>>n;
for(i=0; i<n; i++)
{
cin>>a[i];
stringstream st;
st<<a[i];
s[i]=st.str();

}
for(i=0 ;i<n-1; i++)
{
j=i+1;
while(j<n)
{

    if((s[j]+s[i])>(s[i]+s[j]) )
    {
        swap(s[j],s[i]);
    }
    j++;
}

}
for(i=0; i<n; i++)
{
sum=sum+s[i];
}
cout<<sum<<endl;

istringstream(sum)>>no;
cout<<no;

return 0;

}
the sum variable which is a string is showing the right output but when I am converting that string into integer then the output changes. what problem is there in the code?

@jatin_wadhwa
no need to use the string here, you can simply do it.
Also sort the array first before doing the operation then the comparisions will be correct.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.