Difference between revisions of "NaturalSort"

From Lazarus wiki
Jump to navigationJump to search
Line 18: Line 18:
 
= Features =
 
= Features =
  
  1) Topic enumerators sort till 99 subtopics:
+
===== 1) Topic enumerators sort till 99 subtopics: =====
 
     1
 
     1
 
     1.1.1
 
     1.1.1
Line 25: Line 25:
 
     1.99.99
 
     1.99.99
 
     2
 
     2
  2) Integers sort:
+
===== 2) Integers sort: =====
 
     0
 
     0
 
     00
 
     00
Line 32: Line 32:
 
     2
 
     2
 
     10
 
     10
  3) Floating point numbers sort:
+
===== 3) Floating point numbers sort: =====
 
     0,99
 
     0,99
 
     1
 
     1
Line 38: Line 38:
 
     1,99
 
     1,99
 
     2
 
     2
  Collated alpha sort:
+
===== Collated alpha sort: =====
 
     Alpha sorting task made by OS (Windows and Linux).
 
     Alpha sorting task made by OS (Windows and Linux).
  Thousand separated numbers sort:
+
===== Thousand separated numbers sort: ======
 
     1.198
 
     1.198
 
     1.199
 
     1.199
Line 46: Line 46:
 
     1.200
 
     1.200
 
     1.201
 
     1.201
  IP addresses:
+
===== IP addresses: =====
 
     10.145.254.9
 
     10.145.254.9
 
     10.145.255.9
 
     10.145.255.9

Revision as of 21:01, 25 May 2015

Template:Translate

About

Natural sort order is an ordering of strings in alphabetical order, except that multi-digit numbers are ordered as a single character. Natural sort order has been promoted as being more human-friendly ("natural") than the machine-oriented pure alphabetical order.

For example, in alphabethical sorting "z11" would be sorted before "z2" because "2" is sorted as smaller than "1", while in natural sorting "z2" is sorted as smaller than "z11" because "2" is sorted as smaller than "11".

Functionality to sort by natural sort order is built into many progamming languages and libraries.

Authors

Antônio Galvão and Rik van Kekem

Platforms

Linux and Windows.

Features

1) Topic enumerators sort till 99 subtopics:
    1
    1.1.1
    1.1.2
    1.2.1
    1.99.99
    2
2) Integers sort:
    0
    00
    000
    1
    2
    10
3) Floating point numbers sort:
    0,99
    1
    1,01
    1,99
    2
Collated alpha sort:
    Alpha sorting task made by OS (Windows and Linux).
Thousand separated numbers sort: =
    1.198
    1.199
    1.199,50
    1.200
    1.201
IP addresses:
    10.145.254.9
    10.145.255.9
    10.145.255.10
    10.146.254.9
    121.243.100.0
    255.255.255.254

Thousand and decimal separators are the system default ones.

It is fast. See the time against other functions:

     StrCmpLogicalW: 1
    WideCompareText: 1,238
        NaturalSort: 0,746

Download

The latest version is available here: http://sourceforge.net/projects/lazarusfiles/files/naturalsort.zip/download A demo project is included.

Functions and Procedures

procedure NaturalSort(aList: TStrings);

Example of usage:

 procedure TForm1.Button1Click(Sender :TObject);
 begin
   NaturalSort(Memo1.Lines);
 end;

function UTF8NaturalCompareList(aList: TStringList; Index1, Index2: Integer): Integer;

Example of usage:

 procedure NaturalSort(aList: TStrings);
 var
   L: TStringList;
 begin
   L := TStringList.Create;
   try
     L.Assign(aList);
     L.CustomSort(@UTF8NaturalCompareList);
     aList.Assign(L);
   finally
     L.Free;
   end;
 end;

function UTF8LogicalCompareText(const S1, S2: string): Integer;

function UTF8NaturalCompareText(const S1, S2: string): Integer;