ps has a built-in parameter that sorts its output based on any selected field. you can use
ps k -%cpu -e -o %cpu
Here k sorts the output based on the provided field, and -%cpu - sort it in descending order.
If you omit - before the sort field, it will be sorted in ascending order. Also note that you can give it several sort fields:
ps k -%cpu,-%mem -e -o %cpu,%mem
This sorts the output (in decreasing order for both), first in accordance with the %cpu field, and the second in the %mem field.
n3rV3 source share