<p>I've figured it out</p>
<p>Lables in K8S are for filtering, and not sorting.</p>
<p>I've just added a version field outside labels and it worked.</p>

CC BY-SA 4.0
2022-10-05T17:11:01.967Z
2022-10-05T17:11:01.967Z
Iso404
<p>I need to list some pods sorted by version and get the last index of it.</p>
<p>I'm trying to do like:</p>
<pre><code>kubectl get pods 
--namespace my-namespace
--selector &quot;app.kubernetes.io/name=my-cool-pod&quot;
--sort-by='.items[*].metadata.labels[&quot;app.kubernetes.io/version&quot;]'
--output jsonpath=&quot;{.items[-1:].metadata.name}
</code></pre>
<p>But it is not working.</p>
<p>Also, my pod labels are:</p>
<pre><code>    labels:
      app.kubernetes.io/instance: my-instance
      app.kubernetes.io/managed-by: my-manager
      app.kubernetes.io/name: my-name
      app.kubernetes.io/version: x.y.z
</code></pre>
<p>How to I list pods sorted my version?</p>
<p>Thanks</p>

<kubernetes><kubectl>
How do I list pods sorted by label version in Kubernetes?
Iso404
# How do I list pods sorted by label version in Kubernetes?

(Asked by: username_0 on 2022-09-28)

I need to list some pods sorted by version and get the last index of it.
I'm trying to do like:
kubectl get pods 
--namespace my-namespace
--selector "app.kubernetes.io/name=my-cool-pod"
--sort-by='.items[*].metadata.labels["app.kubernetes.io/version"]'
--output jsonpath="{.items[-1:].metadata.name}

But it is not working.
Also, my pod labels are:
    labels:
      app.kubernetes.io/instance: my-instance
      app.kubernetes.io/managed-by: my-manager
      app.kubernetes.io/name: my-name
      app.kubernetes.io/version: x.y.z

How to I list pods sorted my version?
Thanks

**Tags:** kubernetes, kubectl


---

## 1 answers

### Answer by username_0 on 2022-10-05. Score: 1

I've figured it out

Lables in K8S are for filtering, and not sorting.

I've just added a version field outside labels and it worked.


---

