How to sort with memory/CPU in top command in Linux or Unix?
In this post, we will see how to sort memory and CPU usage in top command output in Linux.

Shift+F to open menu –> Press s to select –> Press Enter to save –> Press ESC to close this menu
1. Run top command and below menu will open.
# top top - 02:05:10 up 11 days, 11:18, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 429 total, 1 running, 428 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.0 sy, 0.0 ni, 99.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 37.7/26389670+[ ] KiB Swap: 0.0/13421772+[ ] PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2343 root 20 0 9015788 42044 3208 S 1.3 0.0 63:31.60 automount 31872 saketj 20 0 144300 2348 1420 R 0.7 0.0 0:05.37 top 2378 root 20 0 3105884 53876 14668 S 0.3 0.0 30:40.69 containerd 1 root 20 0 191140 6244 2432 S 0.0 0.0 5:15.00 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.69 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:04.51 ksoftirqd/0 8 root rt 0 0 0 0 S 0.0 0.0 0:01.83 migration/0
2. Press “Shift+F” to open menu.
s to sort the entries and then select the value with which you want to sort the entries.
Fields Management for window 1:Def, whose current sort field is RES Navigate with Up/Dn, Right selects for move then or Left commits, 'd' or toggles display, 's' sets sort. Use 'q' or to end! * PID = Process Id SWAP = Swapped Size (KiB) * USER = Effective User Name CODE = Code Size (KiB) * PR = Priority DATA = Data+Stack (KiB) * NI = Nice Value nMaj = Major Page Faults * VIRT = Virtual Image (KiB) nMin = Minor Page Faults * RES = Resident Size (KiB) nDRT = Dirty Pages Count * SHR = Shared Memory (KiB) WCHAN = Sleeping in Function * S = Process Status Flags = Task Flags * %CPU = CPU Usage CGROUPS = Control Groups * %MEM = Memory Usage (RES) SUPGIDS = Supp Groups IDs * TIME+ = CPU Time, hundredths SUPGRPS = Supp Groups Names * COMMAND = Command Name/Line TGID = Thread Group Id PPID = Parent Process pid ENVIRON = Environment vars UID = Effective User Id vMj = Major Faults delta RUID = Real User Id vMn = Minor Faults delta RUSER = Real User Name USED = Res+Swap Size (KiB) SUID = Saved User Id nsIPC = IPC namespace Inode SUSER = Saved User Name nsMNT = MNT namespace Inode GID = Group Id nsNET = NET namespace Inode GROUP = Group Name nsPID = PID namespace Inode PGRP = Process Group Id nsUSER = USER namespace Inode TTY = Controlling Tty nsUTS = UTS namespace Inode TPGID = Tty Process Grp Id SID = Session Id nTH = Number of Threads P = Last Used Cpu (SMP) TIME = CPU Time
3. Now the output is sorted.
top - 02:07:00 up 11 days, 11:20, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 430 total, 1 running, 429 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 37.7/26389670+[ ] KiB Swap: 0.0/13421772+[ ] PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 662 root 20 0 292136 178320 169732 S 0.0 0.1 4:33.27 systemd-journal 2341 root 20 0 4883012 97856 25504 S 0.0 0.0 15:05.37 dockerd 2016 root 20 0 734036 83832 82916 S 0.0 0.0 2:29.83 rsyslogd 2378 root 20 0 3105884 53924 14668 S 0.3 0.0 30:41.00 containerd