Operator overloading of < in string

i didnt really understand what does the < do for strings and also why did we pass our bool function to the sort function?What input did it take?

@abhinavgoyal912 “<” is used to compare 2 strings. It compares the strings in alphabetical order. The bool function is for custom comparing. By default strings are sorted in ascending order. This means that if a < b, the function will return true, because that is the order we want. But if want descending order, then if a is less than b, then a < b should return false, right? because that is NOT the order we want.
When you call the sorting function for any container, the compare function takes 2 arguments, they are the elements of that container. In this case we have an array container, and it contains strings. So the function took strings as arguments. Suppose we want to sort a string, eg “apple” to “aelpp” then our container becomes a string, and elements of a string are characters. Therefore the custom sort function will get characters as arguments.

1 Like

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.