1

How to sort integer array in KornShell. Found this link, KornShell Sort Array of Integers but it seems to be not working and throwing error.

Code:

NUM_ARR[1]=-1
NUM_ARR[2]=-2
NUM_ARR[3]=-3
NUM_ARR[4]=-4
NUM_ARR[5]=-5
NUM_ARR[6]=-6
NUM_ARR[7]=-7
for file in /home/fimsctl/datafiles/outbound/timelog/timelog_file_*.csv ; do

    SORTED_NUM_ARR=`($(printf "%s\n" ${NUM_ARR[@]} | sort -n))`
 echo ${SORTED_NUM_ARR[*]}
 done

Output:

testb.ksh[118]: -7:  not found

1 Answer 1

1

You can use sort with process substitution:

sort -n <(printf "%s\n" "${NUM_ARR[@]}")
Sign up to request clarification or add additional context in comments.

4 Comments

Where you've been till now!.. It's like you saved my life. Though it could had been simplest of the questions... Thanks a million!... I still don't get why $(printf "%s\n" ${NUM_ARR[@]} | sort -n) is not working.
You're most welcome. printf "%s\n" "${NUM_ARR[@]}" | sort -n should also work.
how to assign that properly to a array variable?
set -A orderedArray $(printf "%s\n" "${arrayToOrder[@]}" | sort -n | tr "\n" " ");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.