0

The list is such as:

list_unsort  =  v50.0.56.8  v50.0.91.19  v50.0.91.9  v50.0.90.0  v50.0.56.18  v50.0.191.9  v37.4.5.8

try to sort this list

list_sort := $(sort $list_unsort)

Result

list_sort    = v37.4.5.8 v50.0.191.9 v50.0.56.18 v50.0.56.8 v50.0.90.0 v50.0.91.19 v50.0.91.9

As seen, v50.0.56.18 should be sort behind v50.0.56.8. How to sort correct with an easy way? This is in Makefile.

Robert Hu
  • 131
  • 1
  • 5
  • There is no easy way to do it. GNU make's `sort` function is a lexical sort, not a numeric sort, and there's no facility for numeric sorting or even numeric comparison in GNU make. You'll have to do it in a shell script, and even there it's not trivial. – MadScientist Jul 01 '20 at 04:33
  • @MadScientist I answered (the same question from the same poster more or less) [here](https://stackoverflow.com/questions/62650114/apply-the-patches-based-on-openwrt-package-version/62653464#62653464) but the solution seemed to scare him/her away ;) – Vroomfondel Jul 01 '20 at 14:02

1 Answers1

1

If you also have GNU sort available, you can use its -V option to sort the way you want:

Example Makefile:

list_unsort  =  v50.0.56.8  v50.0.91.19  v50.0.91.9  v50.0.90.0  v50.0.56.18  v50.0.191.9  v37.4.5.8

example:
    @echo $(shell printf "%s\n" $(list_unsort) | sort -V)

And demonstration:

$ make -f example.mk
v37.4.5.8 v50.0.56.8 v50.0.56.18 v50.0.90.0 v50.0.91.9 v50.0.91.19 v50.0.191.9
Shawn
  • 47,241
  • 3
  • 26
  • 60
  • Please do not use `shell` functions inside recipes. It may work in many situations but it's a time-bomb waiting for the unwary. A recipe is _already_ run in a shell, so there's no need to use make's `shell` function. – MadScientist Jul 01 '20 at 17:56
  • A simpler way to write this is `@printf '%s\n' $(list_unsort) | sort -V` – MadScientist Jul 01 '20 at 18:00
  • @MadScientist Based on OP's question, they're wanting to set a variable, so that wouldn't work. The important part is the function, not the printing of its output for demonstration purposes. – Shawn Jul 01 '20 at 23:00
  • In that case it would be better to set a variable in your example...? Then it would be immediately applicable and wouldn't showcase non-recommended methods like using `shell` in a recipe... – MadScientist Jul 02 '20 at 01:25
  • @MadScientist The OP has been asking the same thing, in 4 separate questions, more or less since May. This starts to look like trolling. – Vroomfondel Jul 02 '20 at 10:29
  • 4th time's the charm! :) – MadScientist Jul 02 '20 at 20:48