Sign in Join
Wiki copytemporary

NaturalSort: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# About
22
33Natural 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.
44
55For 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".
66
77Functionality to sort by natural sort order is built into many progamming languages and libraries.
88
99# Authors
1010
1111Antônio Galvão and Rik van Kekem
1212
1313# Platforms
1414
1515**Linux** and **Windows**.
1616
1717# Features
1818
1919##### Two types of sorting
2020
21-` type`\
22-`   TSortType = (stNatural, stFloatThousand);`
21+``` pascal
22+ type
23+ TSortType = (stNatural, stFloatThousand);
24+```
2325
2426Topic numerators, integers and IP addresses are sorted by **stNatural** sort type. Thousand separated and floating point numbers are sorted by **stFloatThousand** sort type. Both make collated alphabetical order and sort internal numbers in alphabetical strings.
2527
2628##### Collated alpha-numerical sorting (stNatural or stFloatThousand):
2729
2830Alphabetical parts sorted by OS (Windows or Linux).
2931
30-`    0o3ö6ãrõxögùmî1ó5egõ`\
31-`    1I8iuã0u4ô8EeU7öuOkü`\
32-`    3â0á0e6ùsunueUtö8írò`\
33-`    3áiokúdùsétöbïqãkôvI`\
34-`    4õ4îcë4ò0à1ólOaUpáxã`\
35-`    5î2ãgâoí8î4ü1ò3ïwowë`\
36-`    5úrüqUfò5u0íyïfü5âlí`
32+```text
33+ 0o3ö6ãrõxögùmî1ó5egõ
34+ 1I8iuã0u4ô8EeU7öuOkü
35+ 3â0á0e6ùsunueUtö8írò
36+ 3áiokúdùsétöbïqãkôvI
37+ 4õ4îcë4ò0à1ólOaUpáxã
38+ 5î2ãgâoí8î4ü1ò3ïwowë
39+ 5úrüqUfò5u0íyïfü5âlí
40+```
3741
3842##### Topic enumerators sorting (stNatural):
3943
40-`    1`\
41-`    1.1.1`\
42-`    1.1.2`\
43-`    1.2.1`\
44-`    1.99.99`\
45-`    2`
44+```text
45+ 1
46+ 1.1.1
47+ 1.1.2
48+ 1.2.1
49+ 1.99.99
50+ 2
51+```
4652
4753##### Integers sorting (stNatural):
4854
49-`    0`\
50-`    00`\
51-`    000`\
52-`    1`\
53-`    2`\
54-`    10`
55+```text
56+ 0
57+ 00
58+ 000
59+ 1
60+ 2
61+ 10
62+```
5563
5664##### IP addresses sorting (stNatural):
5765
58-`    10.145.254.9`\
59-`    10.145.255.9`\
60-`    10.145.255.10`\
61-`    10.146.254.9`\
62-`    121.243.100.0`\
63-`    255.255.255.254`
66+```text
67+ 10.145.254.9
68+ 10.145.255.9
69+ 10.145.255.10
70+ 10.146.254.9
71+ 121.243.100.0
72+ 255.255.255.254
73+```
6474
6575##### Floating point numbers sorting (stFloatThousand):
6676
67-`    0,99`\
68-`    1`\
69-`    1,01`\
70-`    1,99`\
71-`    2`
77+```text
78+ 0,99
79+ 1
80+ 1,01
81+ 1,99
82+ 2
83+```
7284
7385##### Thousand separated numbers sorting (stFloatThousand):
7486
75-`    1.198`\
76-`    1.199`\
77-`    1.199,50`\
78-`    1.200`\
79-`    1.201`\
80-`    1.101.300`
87+```text
88+ 1.198
89+ 1.199
90+ 1.199,50
91+ 1.200
92+ 1.201
93+ 1.101.300
94+```
8195
8296Thousand and decimal separators are the system default ones.
8397
8498**It is fast. See the time against other functions:**
8599
86-`     StrCmpLogicalW: 1`\
87-`    WideCompareText: 1,238`\
88-`        NaturalSort: 0,746`
100+```text
101+ StrCmpLogicalW: 1
102+ WideCompareText: 1,238
103+ NaturalSort: 0,746
104+```
89105
90106# Download
91107
92108The latest version is available here: <http://sourceforge.net/projects/lazarusfiles/files/naturalsort.zip/download>
93109
94110A demo project is included.
95111
96112## Functions and Procedures
97113
98114#### procedure NaturalSort(aList: TStrings; SortType: TSortType);
99115
100116This procedure provides a way to sort a list of TStrings directly, according to the chosen SortType, stFloatThousand (for floating point and thousand separators sort) or stNatural (for all other supported sorts).
101117
102118Example of usage:
103119
104-` procedure TForm1.Button1Click(Sender :TObject);`\
105-` begin`\
106-`   NaturalSort(Memo1.Lines, stNatural); `\
107-`   NaturalSort(Memo2.Lines, stFloatThousand); `\
108-` end;`
120+``` pascal
121+ procedure TForm1.Button1Click(Sender :TObject);
122+ begin
123+ NaturalSort(Memo1.Lines, stNatural);
124+ NaturalSort(Memo2.Lines, stFloatThousand);
125+ end;
126+```
109127
110128#### function UTF8NaturalCompareList(aList: TStringList; Index1, Index2: Integer): Integer;
111129
112130Example of usage:
113131
114-` procedure NaturalSort(aList: TStrings);`\
115-` var`\
116-`   L: TStringList;`\
117-` begin`\
118-`   L := TStringList.Create;`\
119-`   try`\
120-`     L.Assign(aList);`\
121-`     L.CustomSort(@UTF8NaturalCompareList);`\
122-`     aList.Assign(L);`\
123-`   finally`\
124-`     L.Free;`\
125-`   end;`\
126-` end;`
132+``` pascal
133+ procedure NaturalSort(aList: TStrings);
134+ var
135+ L: TStringList;
136+ begin
137+ L := TStringList.Create;
138+ try
139+ L.Assign(aList);
140+ L.CustomSort(@UTF8NaturalCompareList);
141+ aList.Assign(L);
142+ finally
143+ L.Free;
144+ end;
145+ end;
146+```
127147
128148#### function UTF8FloatThousandCompareList(aList: TStringList; Index1, Index2: Integer): Integer;
129149
130150Example of usage:
131151
132-` procedure FloatThousandSort(aList: TStrings);`\
133-` var`\
134-`   L: TStringList;`\
135-` begin`\
136-`   L := TStringList.Create;`\
137-`   try`\
138-`     L.Assign(aList);`\
139-`     L.CustomSort(@UTF8FloatThousandCompareList);`\
140-`     aList.Assign(L);`\
141-`   finally`\
142-`     L.Free;`\
143-`   end;`\
144-` end;`
152+``` pascal
153+ procedure FloatThousandSort(aList: TStrings);
154+ var
155+ L: TStringList;
156+ begin
157+ L := TStringList.Create;
158+ try
159+ L.Assign(aList);
160+ L.CustomSort(@UTF8FloatThousandCompareList);
161+ aList.Assign(L);
162+ finally
163+ L.Free;
164+ end;
165+ end;
166+```
145167
146168#### function UTF8FloatThousandCompareText(const S1, S2: string): Integer;
147169
148170#### function UTF8LogicalCompareText(const S1, S2: string): Integer;
149171
150172#### function UTF8NaturalCompareText(const S1, S2: string): Integer;
151173
152174# Natural sort controls
153175
154176A **TNaturalListBox** and a **TNaturalComboBox** are provided along with the demo project.
155177
156178---
157179*From the Free Pascal wiki, page "NaturalSort", as it stood on 2016-11-18 after a change by E-ric, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history