+ 1
how to sort alphabetically
Hello, I am working with C/C++ and I wanted to know if there is a simple way to be able to compare two strings and know which of the two comes first in the alphabet
3 Answers
+ 3
If you are using C++ then std::string class objects have relational operators overloaded accordingly so that you can simply treat them as any other objects while performing comparison between 2 of them.
If you are using C then strcmp () is what you might be looking for.
----
If the main motive of comparing 2 strings lexicographically is to sort a collection of them then as YUGRAJ said, there are factory function for those defined in bot C and C++ standard libraries.
std::sort () in C++
qsort () in C
-----
Useful links :
- std::string relation operators : https://www.cplusplus.com/reference/string/string/operators/
- strcmp () : https://www.cplusplus.com/reference/cstring/strcmp/
- std::sort () : https://www.cplusplus.com/reference/algorithm/sort/
- qsort () : https://www.cplusplus.com/reference/cstdlib/qsort/
+ 1
Do you mean to sort a collection of strings, or compare two strings to see which one was lexicographically greater?
0
Yeah simple sort function on string array Or vector will do that it will give you lexicographically sorted value