You are here

function views_natural_sort_numbers in Views Natural Sort 7.2

Same name and namespace in other branches
  1. 8.2 views_natural_sort.inc \views_natural_sort_numbers()

Transform numbers in a string into a natural sortable string.

Rules are as follows:

  • Embeded numbers will sort in numerical order. The following possibilities are supported

    • A leading dash indicates a negative number, unless it is preceded by a non-whitespace character, which case it is considered just a dash.
    • Leading zeros are properly ignored so as to not influence sort order
    • Decimal numbers are supported using a period as the decimal character
    • Thousands separates are ignored, using the comma as the thous. character
    • Numbers may be up to 99 digits before the decimal, up to the precision of the processor.

Parameters

string $string: The string we wish to transform.

1 string reference to 'views_natural_sort_numbers'
views_natural_sort_get_transformations in ./views_natural_sort.module
Get the full list of transformations to run when saving a natural sort entry.

File

./views_natural_sort.inc, line 98
The Views Natural Sort module include file.

Code

function views_natural_sort_numbers($string) {

  // Find an optional leading dash (either preceded by whitespace or the first
  // character) followed by either:
  // - an optional series of digits (with optional embedded commas), then a
  //   period, then an optional series of digits
  // - a series of digits (with optional embedded commas)
  return preg_replace_callback('/(\\s-|^-)?(?:(\\d[\\d,]*)?\\.(\\d+)|(\\d[\\d,]*))/', '_views_natural_sort_number_transform_match_callback', $string);
}