On Monday, 18 May 2015 07:45:19 UTC+3,
udt...@gmail.com wrote:
> I would like to have sort function for strings that would first
> compare length and have longer string be greater then shorter one, and
> if they are same length - compare them as usual. So that
>
> 1. "1234567"
> 2. "12345"
> 3. "111111"
> 4. "13456"
> 5. "01102121"
>
> would get sorted as
> 5 > 1 > 3 > 4 > 2
>
> bool less(const string& lhs, const string&rhs)
> {
> // if( lhs.length() < rhs.length() ) return true;
> return (lhs.length() < rhs.length()) ? true : (lhs < rhs);
> }
So both 'less( "1234567", "13456" )' and
'less( "13456", "1234567" )' return 'true'?
> Is there a more efficient way to do this ?
Why you care about efficiency when your algorithm seems incorrect?
Most important goal is always correctness since no one needs
software that gives incorrect answers more quickly.