c++ custom sorting a vector -


not going go deep doing because homework , don't need done me but, need help. need able specify part of vector<vector<string>> gets sorted first , under parameters.

currently doing works calling

sort ( v.begin(), v.end() ); 

if write out vectors like:

5 2 4 6 12 2 5

22 51 2 5 72 1

and might need sort in descending order 2nd column , if 2nd column same sort next specified column.

called ./sort 2,4

would sort second column , 4th.

i looked around , apart writing own sorting algorithm don't know how customize sort.

std::sort() has second form takes compare functor third parameter. lets control ordering of sort without having write sort algorithm yourself.

the function provide given 2 objects, , must return true if first object should ordered before second ("less than") , false otherwise.

e.g.:

std::sort(v.begin(), v.end(), [](const vector<string>& v1, const vector<string>& v2) {     // return true if v1 < v2, false otherwise }); 

(of course, it's work out logic need sort ordered how want it.)


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

python - How to remove the Xframe Options header in django? -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -