Skip to content

Instantly share code, notes, and snippets.

@bluebat
Created February 14, 2014 09:39
Show Gist options
  • Save bluebat/8998367 to your computer and use it in GitHub Desktop.
Save bluebat/8998367 to your computer and use it in GitHub Desktop.
Quick Sort in Makefile
#!/usr/bin/gmake -sf
#modified after jserv's Makefile.qsort from
#http://blog.linux.org.tw/~jserv/archives/002035.html
gt = $(shell if [ $1 -gt $2 ] ; then echo TRUE; fi)
lt = $(shell if [ $1 -lt $2 ] ; then echo TRUE; fi)
le = $(shell if [ $1 -le $2 ] ; then echo TRUE; fi)
define qsort
$(if $(call le,$(words $1),1),$1, \
$(call qsort, \
$(foreach i,$1, \
$(if $(call gt,$(firstword $1),$i), $i,))) \
$(firstword $1) \
$(call qsort, \
$(foreach i,$1, \
$(if $(call lt,$(firstword $1),$i), $i,))))
endef
ifeq "$(MAKECMDGOALS)" ""
$(info Usage: qsort.mk num1 num2 num3 ...)
else
$(info $(strip $(call qsort, $(MAKECMDGOALS))))
endif
%:
@true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment