I need help asap plz

Write a void function named sort2 that takes two string parameters and sorts them, alphabetically. The function swaps the values of the string parameters if the 1st string is lexicographically greater than the 2nd string and otherwise leaves the strings unchanged. For example:string s1 = "Orange";string s2 = "Apple";sort2(s1, s2); // now s1 is "Apple" and s2 is "Orange"
void sort2(string &s1, string &s2)
{
if (string s1 > string s2)
{
string tmp = s1;
s1 = s2;
s2 = tmp;
}
else
{
return s1;
return s2;
}
that is what i had so far
Your function has a return type of void, so you cannot return anything from your function.

1
2
3
4
5
6
7
8
9
void sort2(string& a, string& b)
{
    if ( a > b )
    {
        string temp = a ;
        a = b ;
        b = temp ;
    }
}


should be sufficient. You don't need to return anything. The parameters are passed by reference and modified in the function, so the change will be reflected in the calling code.
Topic archived. No new replies allowed.