Sayan
Sayan

Reputation: 2724

std::sort on std::vector<std::string>

I have a std::vector<std::string> which would contain numbers and characters (single char). I would want to have numbers sorted first followed by the characters...so I have a jumbled up vector of strings as an input and after sort I want it like 1,2,3,5,7,9,10,A,B,C,D. But I guess sort also compares the sizes of the inputs, and hence if my vector has numbers of different lengths, I am getting a wrong output. For instance, doing an std::sort (vec.begin(),vec.end()) on 9,4,5,2,10,11,A,D,B,E,C returns 10,11,2,4,5,9,A,B,C,D,E.

How do I rectify my mistake and what am I missing?

Thank you,
Sayan

Upvotes: 0

Views: 612

Answers (1)

Edward Strange
Edward Strange

Reputation: 40859

Write a non-lexicographical comparison routine and pass it along with the iterators to std::sort.

Upvotes: 1

Related Questions