Sorting a list of numbers numerically in CMake

Viewed 256

I have a variable containing a list of version numbers:

1.1;10.0;11.0;12.0;12.1;2.0;2.1;3.0;3.1;3.2;4.0;4.1;5.0;5.1;5.2;6.0;6.1;6.2;6.3;7.0;8.0;9.0

As you can see, the list is sorted alphabetically. I want the list to be sorted numerically:

1.1;2.0;2.1;3.0;3.1;3.2;4.0;4.1;5.0;5.1;5.2;6.0;6.1;6.2;6.3;7.0;8.0;9.0;10.0;11.0;12.0;12.1

Is there an easy way to do this in CMake?

1 Answers

EDIT: As of CMake 3.18, there is built-in functionality to sort numbers numerically. You can use the list(SORT ...) command with NATURAL ordering:

set(sort_list 1.1;10.0;11.0;12.0;12.1;2.0;2.1;3.0;3.1;3.2;4.0;4.1;5.0;5.1;5.2;6.0;6.1;6.2;6.3;7.0;8.0;9.0)
list(SORT sort_list COMPARE NATURAL)

to sort the list numberically.


For CMake 3.17 and lower, there is no built-in numeric sort, so you have to write your own. Here is one I just wrote up based on the recursive insertion sort algorithm described here:

function(insertionSortRecursive sort_list len)
  # Base case, return.
  if(${len} LESS_EQUAL 1)
    return()
  endif()

  math(EXPR len_minus_one "${len} - 1")
  # Recursively sort the sublist of size (len - 1).
  insertionSortRecursive("${sort_list}" ${len_minus_one})
  set(_sort_list ${sort_list})

  # Get the last element in the sublist.
  list(GET _sort_list ${len_minus_one} last_element)
  # Define a counter for the sublist we will operate on.
  math(EXPR sublist_counter "${len} - 2")
  # Get the element at the counter index.
  list(GET _sort_list ${sublist_counter} counter_element)

  # Loop to move those elements greater than last_element up one position.
  while((${sublist_counter} GREATER_EQUAL 0) AND (${counter_element} GREATER ${last_element}))
    # Move elem at counter index up one position in list.
    math(EXPR counter_plus_one "${sublist_counter} + 1")
    list(REMOVE_AT _sort_list ${counter_plus_one})
    list(INSERT _sort_list ${counter_plus_one} ${counter_element})
    # Decrement the sublist counter.
    math(EXPR sublist_counter "${sublist_counter} - 1")
    # Get the element at the new counter value.
    list(GET _sort_list ${sublist_counter} counter_element)
  endwhile()

  # Place the last element at the correct position.
  math(EXPR counter_plus_one "${sublist_counter} + 1")
  list(REMOVE_AT _sort_list ${counter_plus_one})
  list(INSERT _sort_list ${counter_plus_one} ${last_element})
  # Send the modified list back up to parent scope.
  set(sort_list ${_sort_list} PARENT_SCOPE)    
endfunction()

Just pass the unsorted list to this insertionSortRecursive() CMake function to sort it:

set(sort_list 1.1;10.0;11.0;12.0;12.1;2.0;2.1;3.0;3.1;3.2;4.0;4.1;5.0;5.1;5.2;6.0;6.1;6.2;6.3;7.0;8.0;9.0)
message("Unsorted: ${sort_list}")

# Get the list length
list(LENGTH sort_list LIST_LEN)
# Call the recursive sort function.
insertionSortRecursive("${sort_list}" ${LIST_LEN})

message("Sorted:   ${sort_list}")

This prints the following, verifying the list is sorted:

Unsorted: 1.1;10.0;11.0;12.0;12.1;2.0;2.1;3.0;3.1;3.2;4.0;4.1;5.0;5.1;5.2;6.0;6.1;6.2;6.3;7.0;8.0;9.0
Sorted:   1.1;2.0;2.1;3.0;3.1;3.2;4.0;4.1;5.0;5.1;5.2;6.0;6.1;6.2;6.3;7.0;8.0;9.0;10.0;11.0;12.0;12.1

There are some other numeric sorting implementations in CMake out there, such as this Insertion Sort and this Bubble Sort.

Related