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.

>>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.