It cant able to sort the $q value. I tried on -nr option but still the same. For your info, the $q value is actually a floating point. I would like to sort it accordingly.
If I understand your question correctly Echo is processing one line at a time so you are sorting one line then outputting it to a file which really has no effect with the sort.
Create the file then sort it with sort -rn -o test.txt test.txt
Did you read the reply you got already? You're not sorting the file, you are sorting one line at a time, which is not altogether productive. You need to move the sorting to a later stage.