subroutine idxPreSort(iCI, idxAlphaBetaOrbs, ex, signCI)
use util_mod, only: swap
integer, intent(in) :: iCI, idxAlphaBetaOrbs(nbasis)
integer, intent(out) :: signCI
integer, intent(inout) :: ex(2, n_store_ci_level)
integer :: j, k, p
! indices conversion
do k = 1, iCI
do p = 1, nel
if (ex(1, k) == idxAlphaBetaOrbs(p)) then
ex(1, k) = p
exit
end if
end do
do p = nel + 1, nbasis
if (ex(2, k) == idxAlphaBetaOrbs(p)) then
ex(2, k) = p - nel
exit
end if
end do
end do
! insertion sort
signCI = 1
do p = 1, 2
do k = 2, iCI
do j = k, 2, -1
if (ex(p, j) < ex(p, j - 1)) then
call swap(ex(p, j), ex(p, j - 1))
! minus sign for odd number of permutations
signCI = -signCI
else
exit
end if
end do
end do
end do
end subroutine idxPreSort