I have asked this question before and have not gotten a completely useful answer. I am trying to sort a number of any size. For example: if 643597 is entered, the output should be 345679.

Dani AI

Generated

A few practical notes and a compact alternative that avoid integer overflow and scale to very large inputs.

Treat the input as text (std::string) so any length number is supported. The approaches already mentioned in the replies are valid: splitting into digits (as and suggested) or using the STL sort on a character buffer (as pointed out). If the input consists only of digits, a counting-sort variant is simpler and linear time because there are only 10 possible digit values. The snippet below reads a number as a string, handles an optional leading minus, validates digits, counts occurrences of 0–9, and prints the digits in ascending order while avoiding unwanted leading zeros.

#include <iostream>
#include <string>
#include <array>
#include <cctype>

int main() {
    std::string s;
    if (!(std::cin >> s)) return 0;

    bool negative = false;
    if (!s.empty() && s[0] == '-') { negative = true; s.erase(s.begin()); }

    for (char c : s)
        if (!std::isdigit(static_cast<unsigned char>(c))) {
            std::cerr << "Non-digit in input\n"; return 1;
        }

    std::array<int,10> cnt = {0};
    for (char c : s) ++cnt[c - '0'];

    std::string out;
    for (int d = 0; d <= 9; ++d) out.append(cnt[d], char('0' + d));

    // strip leading zeros unless the number is zero
    std::size_t p = out.find_first_not_of('0');
    if (p == std::string::npos) out = "0"; else out = out.substr(p);

    if (negative && out != "0") std::cout << '-';
    std::cout << out << '\n';
    return 0;
}

Notes: this prints digits in ascending numeric order (e.g., 643597 -> 345679). If you want descending order, iterate digits 9..0. Decide how to treat leading zeros depending on whether you want a numeric representation or a raw digit sort. Use the STL sort approach when you need a one-liner or when sorting non-digit characters; use counting sort for maximum speed and minimal memory when inputs are digit-only.

Recommended Answers

All 4 Replies

>>I have asked this question before and have not gotten a completely useful answer.

I saw your previous post, and also the answers to that post. There were some useful ideas, that you could apply - store each digit in an array; as long as you use int values, the size of your array will not be bigger than 10...etc, etc...

this is jst my quiz in the class today.. by using 2 loop and swap the lower number ...

int a[arraysize],hold
.
.
.


for (i=0;i<arraysize-1;i++)
for (j=0; j<arraysize-1;j++)
if (a>a[i+1])
{ hold = a;
a=a[i+1];
a[i+1]=hold;
}

I have asked this question before and have not gotten a completely useful answer. I am trying to sort a number of any size. For example: if 643597 is entered, the output should be 345679.

Heres a simple solution using the stl


#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
//sort using a char array.
char cArr[] = "15462";
int len = strlen(cArr);

std::sort(cArr, cArr + len);

std::cout<<"Sorted char array is: "<<cArr<<"\n";

//or we can sort on vector or any other container
std::vector<int> num;

//create number 15462
num.push_back(1);
num.push_back(5);
num.push_back(4);
num.push_back(6);
num.push_back(2);

std::sort(num.begin(),num.end());

std::cout<<"Sorted vector is: ";
for(int i = 0; i<num.size(); ++i)
{
std::cout<<num;
}
return 0;
}

thanks. I ended up using a character array. It works great!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.