The straight-forward approach is to have your sortList
function take a custom comparison function. Your custom comparison function should return false if the left operand is an empty string ""
. If the left operand is not empty but the right operand is empty than return true. Otherwise just do a regular string comparison.
For example, using std::string
this comparison function should do the trick:
bool comp(const std::string &lhs, const std::string &rhs)
{
if (lhs == "") return false;
return rhs == "" || !(rhs < lhs);
}
Note that you'll need to fix your sortList
to swap words instead of letters to get what you're looking for.