Jahirul Islam Monir Jahirul Islam Monir - 3 months ago 7
C++ Question

How can i sort a string with integers in c++?

I also interested with built in std::qsort.. but couldn't understand the functionalities of std::qsort..

922003001020293839297830207344987344973074734
766352786207892397340783784078348747606208602
182823068326283756515117829362376823572395775
//the string size of all is equal
//i want to sort this kind of big numbers.


Anyone please explain the better way for sorting.

Answer

Here it's done with std::sort from the header algorithm

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

int main(){

    std::vector<std::string> nums{
        "922003001020293839297830207344987344973074734",
        "766352786207892397340783784078348747606208602",
        "182823068326283756515117829362376823572395775"
    };


    std::cout << "unsorted: " << std::endl;
    for (auto i : nums){
        std::cout << i << std::endl;
    }

    std::sort(nums.begin(), nums.end()); //sort it


    std::cout << "\nsorted: " << std::endl;
    for (auto i : nums){
        std::cout << i << std::endl;
    }

    system("pause");
    return 0;
}

output:

unsorted: 
922003001020293839297830207344987344973074734
766352786207892397340783784078348747606208602
182823068326283756515117829362376823572395775

sorted: 
182823068326283756515117829362376823572395775
766352786207892397340783784078348747606208602
922003001020293839297830207344987344973074734