Using --sort-by with kubectl get pods --all-namespaces to sort by both namespace AND name doesn't work
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
kubectl --sort-by only sorts by one field. If you are listing pods across all namespaces and want namespace first and pod name second, --sort-by alone cannot express that multi-key ordering.
What --sort-by Can Actually Do
This works:
And this works:
But kubectl does not support a composite sort such as "namespace, then name" through one --sort-by expression.
That is why attempts to combine both fields do not behave the way people expect.
Why It Fails
--sort-by expects a single JSONPath expression. It is not a SQL-style ORDER BY field1, field2 feature. Even though the data contains both namespace and name, the built-in sorting logic only uses one extracted key.
So the answer to the original problem is simple:
- '
kubectl --sort-bysupports one sort key' - multi-field sorting requires a second tool or custom formatting
Practical Shell Solution
If you are working in a Unix-like shell, the usual answer is to let kubectl print the fields and then use sort for the two-key ordering:
In the default kubectl get pods -A table output:
- column 1 is the namespace
- column 2 is the pod name
So sort -k1,1 -k2,2 gives the expected namespace-then-name ordering.
A More Explicit Custom-Columns Version
If you want to avoid depending on the default table format, print only the columns you care about:
This is more stable because you control the output columns directly.
JSON and jq for Structured Sorting
If you prefer structured processing over text sorting, use JSON output and jq:
This is often the cleaner choice for scripts because it does not depend on terminal table formatting.
When --sort-by Is Still Useful
--sort-by is still fine when one field is enough. For example:
- sort all pods by creation time
- sort all pods by name only
- sort all pods by namespace only
The limitation appears only when you need lexicographic ordering across multiple fields.
That is also why there is no reliable multi-key workaround hidden inside one JSONPath expression. Once you need primary and secondary ordering, you need either a second sorting tool or a structured post-processing step.
The exact second-stage tool can vary by environment. On Unix-like shells, sort is the usual answer. In more script-heavy workflows, JSON output plus jq is often the more maintainable option.
Common Pitfalls
- Assuming
--sort-byaccepts more than one key. - Trying to cram multiple fields into one JSONPath and expecting composite sorting.
- Using default table output in scripts without realizing column order can be a fragile dependency.
- Forgetting
--no-headersbefore piping intosort. - Using text sorting when structured JSON output would make the script clearer and safer.
Summary
- '
kubectl --sort-bysupports only a single sort field.' - To sort by namespace and then pod name, pipe the output to
sortor usejq. - '
kubectl get pods -A --no-headers | sort -k1,1 -k2,2is the simplest shell solution.' - '
custom-columnsor JSON output makes automation more robust.' - The limitation is in
kubectlsorting behavior, not in your understanding of namespaces or pod names.

