SQLite Forum

Natural sort order
Here is a revised "natural sort" collating function.  The name is
now changed to "NATSORT", because I found a PHP and a Python module
that also use that name.

The previous implementation sorted embedded unsigned integers in
numeric order _and last_.  This implementation sorts embedded unsigned
integers in numeric order, but otherwise in ASCII order.

Please test and report back if this collating sequence is useful.
If it is, perhaps it might land in the next release of SQLite.

# The Code

** 2020-03-27
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
** Implement a collating sequence that sorts embedded unsigned integers
** in numeric order.
#include "sqlite3ext.h"
#include <ctype.h>

** Collating function that compares text byte-by-byte but compares
** digits in numeric order.
static int natSortCollFunc(
  void *notUsed,
  int nKey1, const void *pKey1,
  int nKey2, const void *pKey2
  const unsigned char *zA = (const unsigned char*)pKey1;
  const unsigned char *zB = (const unsigned char*)pKey2;
  int i, x;
  for(i=0; i<nKey1 && i<nKey2; i++){
    x = zA[i] - zB[i];
    if( x!=0 ){
      int j;
      for(j=i; j<nKey1 && j<nKey2 && isdigit(zA[j]) && isdigit(zB[j]); j++){}
      if( i==j && (i==0 || !isdigit(zA[i-1])) ){
        return x;
      }else if( j<nKey1 && isdigit(zA[j]) ){
        return +1;
      }else if( j<nKey2 && isdigit(zB[j]) ){
        return -1;
        return x;
  return nKey1 - nKey2;

#ifdef _WIN32
int sqlite3_natsort_init(
  sqlite3 *db, 
  char **pzErrMsg, 
  const sqlite3_api_routines *pApi
  int rc = SQLITE_OK;
  (void)pzErrMsg;  /* Unused parameter */
  rc = sqlite3_create_collation(db, "natsort", SQLITE_UTF8, 0, natSortCollFunc);
  return rc;