Extract the diagonal entries, find the indices corresponding to sorting those, and use those indices to rearrange an identity matrix into a permutation matrix.
%matrix size
N = 5;
%random diagonal matrix
d=rand(N,1);
D = diag(d);
%extract the diagonal entries of D and sort them
[~,I]=sort(diag(D));
%generate the permutation matrix
P = eye(size(D));
P = P(I,:)
%Verify answer P*D gives the sorted matrix
P*D