Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 611

jq sort by version as string

$
0
0

I'm trying to sort the following json reponse to pick the latest version:

[    {"TagVersion": "1.0.11"    },    {"TagVersion": "1.1.8"    },    {"TagVersion": "1.0.10",    },    {"TagVersion": "1.0.9",    },    {"TagVersion": "1.0.77"    }]

Correct sorting should be:

    {"TagVersion": "1.0.9",    },    {"TagVersion": "1.0.10",    },    {"TagVersion": "1.0.11"    },    {"TagVersion": "1.0.77"    },    {"TagVersion": "1.1.8"    }

I'm currently able to do part of the job. It's working for simple cases (all version part major/minor/bug has the same number of digits).

jq -r [.[]]|max_by(.TagVersion|split(".") | map(tonumber)

The best way to do it in my mind should be to add a multiplication the each part. Example:

# With condition than every "part" as a maximum of 2 digits. It won't work with 3 digits# Version 1.23.871 * 1000 + 23 * 10 + 87 = 1317# Version 3.0.01 * 1000 + 0 * 10 + 0 = 3000# Version 1.89.781 * 1000 + 89*10 + 78 = 1968

Does anybody have an idea to implement this? 🙂


Viewing all articles
Browse latest Browse all 611

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>