I wanted to know how I can sort a string vector such that the string with the least amount of characters is on top of the vector. For instance if the vector has ABCD,ABCDE,ABC in it. ABC gets to the top.I would be interested to know how this could be achieved with sort_if and what the predicate would look like ? Any other methods are also welcome
Asked
Active
Viewed 3.5k times
17
-
The is no `std::sort_if()`. There is an overload of `std::sort()` taking predicate, though. The predicate would, obviously, compare the length of arguments and if they are equal compare the lexicographical order of the strings; otherwise it would return `true` if the first argument is shorter. – Dietmar Kühl Sep 16 '13 at 15:18
-
FYI - these answers are now very outdated, no need for an overloaded operator now, simply use a lambda! – Babra Cunningham Oct 15 '16 at 10:11
-
Example for sorting using lambda expression: `std::sort(words.begin(), words.end(), [](std::string a, std::string b) {return a.length() < b.length(); });` – hmofrad Mar 12 '20 at 15:43
3 Answers
26
Make your own custom functor to compare the size of string(s) and use that to sort the strings.
struct compare {
inline bool operator()(const std::string& first,
const std::string& second) const
{
return first.size() < second.size();
}
};
std::vector<std::string> v;
compare c;
std::sort(v.begin(), v.end(), c);
In modern c++ we can use a lambda to do the same
std::vector<std::string> v;
std::sort(v.begin(), v.end(), []
(const std::string& first, const std::string& second){
return first.size() < second.size();
});

andre
- 7,018
- 4
- 43
- 75
-
3You probably want to order the strings lexicographically if they are of the same length. The function call operator should probably be `const`. – Dietmar Kühl Sep 16 '13 at 15:20
-
You don't need to make a functor to do this. A simple comparison function will do it as well. – Zac Howland Sep 16 '13 at 15:20
-
@DietmarKühl I'd think you just want to be stable if they are the same length. Guess that's up to the asker. – BoBTFish Sep 16 '13 at 15:22
-
1@ZacHowland: Although semantically this is true, in practice the function object can be inlined while using the function pointer as predicate it often cannot be inlined. – Dietmar Kühl Sep 16 '13 at 15:22
-
That is no longer true (and hasn't been for a while actually): http://stackoverflow.com/questions/4860762/c-can-compilers-inline-a-function-pointer – Zac Howland Sep 16 '13 at 15:27
7
Should be able to use regular std::sort(first, last, compare)
, and a compare function like this:
bool compareLen(const std::string& a, const std::string& b)
{
return (a.size() < b.size());
}

Mats Petersson
- 126,704
- 14
- 140
- 227
-
This compareLen is not working for me. I have used your function and invoked it by the line sort(words.begin(), words.end(), compareLen); – Ganesh M S Aug 07 '19 at 15:36
2
std::sort
takes an optional argument for a custom comparison
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
You can just define a function that compares based on the length.

Ari
- 1,102
- 9
- 17