Sorting an integer in descending order

Hello,
I need to sort in a descending order a four digit integer, without using arrays. The idea is that the function just takes the original integer for example int1=1234, and returns another integer int2=4321; and also it has to manage leading zeros in the original integer for example int3=0213. I don't know how to do it. I would appreciate it a lot if you could help me. Thanks in advance.
> I don't know how to do it. I would appreciate it a lot if you could help me.

When a problem appears to be too complex, try to break it up into many small sub-problems, each one of which is individually quite simple.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>

// split into digits ; abcd < 10'000 (four or less digits)
void split( unsigned int abcd, int& a, int& b, int& c, int& d )
{
    d = abcd % 10 ;
    c = abcd/10 % 10 ;
    b = abcd/100 % 10 ;
    a = abcd/1000 % 10 ;
}

// a, b, c, d in [0,9] (digits); combine to get integer abcd
unsigned int combine( int a, int b, int c, int d ) { return d + c*10 + b*100 + a*1000 ; }

// note: all overloads of sort sort in descending order

void sort( int& a, int& b ) { if( a < b ) { int temp = a ; a = b ; b = temp ; } }

void sort( int& a, int& b, int& c ) { sort(a,b) ; sort(b,c) ; sort(a,b) ; }

void sort( int& a, int& b, int& c, int& d ) { sort(a,b,c) ; sort(c,d) ; sort(a,b,c) ; }

// abcd < 10'000 (four or less digits) ; sort digits in descending order
unsigned int sort_digits( unsigned int abcd )
{
    int a, b, c, d ;
    split( abcd, a, b, c, d ) ;
    sort( a, b, c, d ) ;
    return combine( a, b, c, d ) ;
}

int num_digits( unsigned int n )
{
    if( n < 10 ) return 1 ;

    int ndigits = 0 ;
    for( ; n != 0 ; n /= 10 ) ++ndigits ;
    return ndigits ;
}

// trim to most significant ndigits
unsigned int trim( unsigned int n, int ndigits )
{
    while( num_digits(n) > ndigits ) n /= 10 ;
    return n ;
}

// n has four or less digits ; sort digits in descending order
unsigned int sort( unsigned int n )
{
    n %= 10000 ; // abundant caution
    return trim( sort_digits(n), num_digits(n) ) ;
}

int main()
{
    for( unsigned int v : { 0, 1, 10, 17, 107, 170, 1070, 7010, 1007, 1700, 1722, 6366, 5555 } )
        std::cout << v << ' ' << sort(v) << '\n' ;
}

http://coliru.stacked-crooked.com/a/3542c2d81c3162bd
In search of the most obscure code ever written:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

int ten( int p )
{
   int ans = 1;
   while ( p-- ) ans *= 10;
   return ans;
}

int main()
{
   unsigned int N;
   cout << "Input N: ";
   cin >> N;

   int a = N / 1000;   N %= 1000;
   int b = N / 100 ;   N %= 100 ;
   int c = N / 10  ;   N %= 10  ;
   int d = N;

   int pa = ( a >= b ) + ( a >= c ) + ( a >= d );
   int pb = ( b >  a ) + ( b >= c ) + ( b >= d );
   int pc = ( c >  a ) + ( c >  b ) + ( c >= d );
   int pd = ( d >  a ) + ( d >  b ) + ( d >  c );

   N = a * ten( pa ) + b * ten( pb ) + c * ten( pc ) + d * ten( pd );

   cout << "Ordered: " << N;
}


Input N: 1234
Ordered: 4321


Input N: 0213
Ordered: 3210
Topic archived. No new replies allowed.