NaturalSort: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: NaturalSort
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # About | |
| 2 | + | ||
| 3 | + | 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. | |
| 4 | + | ||
| 5 | + | 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". | |
| 6 | + | ||
| 7 | + | Functionality to sort by natural sort order is built into many progamming languages and libraries. | |
| 8 | + | ||
| 9 | + | # Authors | |
| 10 | + | ||
| 11 | + | Antônio Galvão and Rik van Kekem | |
| 12 | + | ||
| 13 | + | # Platforms | |
| 14 | + | ||
| 15 | + | **Linux** and **Windows**. | |
| 16 | + | ||
| 17 | + | # Features | |
| 18 | + | ||
| 19 | + | ##### Two types of sorting | |
| 20 | + | ||
| 21 | + | ` type`\ | |
| 22 | + | ` TSortType = (stNatural, stFloatThousand);` | |
| 23 | + | ||
| 24 | + | Topic 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. | |
| 25 | + | ||
| 26 | + | ##### Collated alpha-numerical sorting (stNatural or stFloatThousand): | |
| 27 | + | ||
| 28 | + | Alphabetical parts sorted by OS (Windows or Linux). | |
| 29 | + | ||
| 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í` | |
| 37 | + | ||
| 38 | + | ##### Topic enumerators sorting (stNatural): | |
| 39 | + | ||
| 40 | + | ` 1`\ | |
| 41 | + | ` 1.1.1`\ | |
| 42 | + | ` 1.1.2`\ | |
| 43 | + | ` 1.2.1`\ | |
| 44 | + | ` 1.99.99`\ | |
| 45 | + | ` 2` | |
| 46 | + | ||
| 47 | + | ##### Integers sorting (stNatural): | |
| 48 | + | ||
| 49 | + | ` 0`\ | |
| 50 | + | ` 00`\ | |
| 51 | + | ` 000`\ | |
| 52 | + | ` 1`\ | |
| 53 | + | ` 2`\ | |
| 54 | + | ` 10` | |
| 55 | + | ||
| 56 | + | ##### IP addresses sorting (stNatural): | |
| 57 | + | ||
| 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` | |
| 64 | + | ||
| 65 | + | ##### Floating point numbers sorting (stFloatThousand): | |
| 66 | + | ||
| 67 | + | ` 0,99`\ | |
| 68 | + | ` 1`\ | |
| 69 | + | ` 1,01`\ | |
| 70 | + | ` 1,99`\ | |
| 71 | + | ` 2` | |
| 72 | + | ||
| 73 | + | ##### Thousand separated numbers sorting (stFloatThousand): | |
| 74 | + | ||
| 75 | + | ` 1.198`\ | |
| 76 | + | ` 1.199`\ | |
| 77 | + | ` 1.199,50`\ | |
| 78 | + | ` 1.200`\ | |
| 79 | + | ` 1.201`\ | |
| 80 | + | ` 1.101.300` | |
| 81 | + | ||
| 82 | + | Thousand and decimal separators are the system default ones. | |
| 83 | + | ||
| 84 | + | **It is fast. See the time against other functions:** | |
| 85 | + | ||
| 86 | + | ` StrCmpLogicalW: 1`\ | |
| 87 | + | ` WideCompareText: 1,238`\ | |
| 88 | + | ` NaturalSort: 0,746` | |
| 89 | + | ||
| 90 | + | # Download | |
| 91 | + | ||
| 92 | + | The latest version is available here: <http://sourceforge.net/projects/lazarusfiles/files/naturalsort.zip/download> | |
| 93 | + | ||
| 94 | + | A demo project is included. | |
| 95 | + | ||
| 96 | + | ## Functions and Procedures | |
| 97 | + | ||
| 98 | + | #### procedure NaturalSort(aList: TStrings; SortType: TSortType); | |
| 99 | + | ||
| 100 | + | This 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). | |
| 101 | + | ||
| 102 | + | Example of usage: | |
| 103 | + | ||
| 104 | + | ` procedure TForm1.Button1Click(Sender :TObject);`\ | |
| 105 | + | ` begin`\ | |
| 106 | + | ` NaturalSort(Memo1.Lines, stNatural); `\ | |
| 107 | + | ` NaturalSort(Memo2.Lines, stFloatThousand); `\ | |
| 108 | + | ` end;` | |
| 109 | + | ||
| 110 | + | #### function UTF8NaturalCompareList(aList: TStringList; Index1, Index2: Integer): Integer; | |
| 111 | + | ||
| 112 | + | Example of usage: | |
| 113 | + | ||
| 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;` | |
| 127 | + | ||
| 128 | + | #### function UTF8FloatThousandCompareList(aList: TStringList; Index1, Index2: Integer): Integer; | |
| 129 | + | ||
| 130 | + | Example of usage: | |
| 131 | + | ||
| 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;` | |
| 145 | + | ||
| 146 | + | #### function UTF8FloatThousandCompareText(const S1, S2: string): Integer; | |
| 147 | + | ||
| 148 | + | #### function UTF8LogicalCompareText(const S1, S2: string): Integer; | |
| 149 | + | ||
| 150 | + | #### function UTF8NaturalCompareText(const S1, S2: string): Integer; | |
| 151 | + | ||
| 152 | + | # Natural sort controls | |
| 153 | + | ||
| 154 | + | A **TNaturalListBox** and a **TNaturalComboBox** are provided along with the demo project. | |
| 155 | + | ||
| 156 | + | --- | |
| 157 | + | *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).* |
