tags:

views:

84

answers:

1

Hello,

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

+1  A: 

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

Noah Roberts
I guess a lexicographical sort uses ascii to compare as it's default. Thanks, would write a custom one as you suggested.
Sayan