Prolog, 177/302 characters
I'm a beginner on Prolog, so probably this is not the most condensed code.
:- use_module(library(clpfd)).
sort(N) :-
N = [N0,N1,N2,N3,N4,N5,N6,N7,N8,N9],
domain([N0],1,9),
domain([N1,N2,N3,N4,N5,N6,N7,N8,N9],0,9),
all_different(N),
labeling([],N).
Returns:
| ?- sort2(N).
N = [1,0,2,3,4,5,6,7,8,9] ? ;
N = [1,0,2,3,4,5,6,7,9,8] ? ;
N = [1,0,2,3,4,5,6,8,7,9] ? ;
N = [1,0,2,3,4,5,6,8,9,7] ? ;
N = [1,0,2,3,4,5,6,9,7,8] ?
yes
If you want it to return an integer:
:- use_module(library(clpfd)).
sort(M) :-
N = [N0,N1,N2,N3,N4,N5,N6,N7,N8,N9],
domain([N0],1,9),
domain([N1,N2,N3,N4,N5,N6,N7,N8,N9],0,9),
all_different(N),
labeling([],N),
M is (N0*1000000000)+(N1*100000000)+(N2*10000000)+(N3*1000000)+
(N4*100000)+(N5*10000)+(N6*1000)+(N7*100)+(N8*10)+N9.
Returns:
| ?- sort(N).
N = 1023456789 ? ;
N = 1023456798 ? ;
N = 1023456879 ? ;
N = 1023456897 ? ;
N = 1023456978 ?
yes
Using instead:
labeling([down],N)
Gives the numbers in the opposite order:
| ?- sort(N).
N = 9876543210 ? n
N = 9876543201 ? n
N = 9876543120 ? n
N = 9876543102 ? n
N = 9876543021 ?
yes
Unlike some other codes posted, this returns all possibilities (with no repetitions).