Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Natural sort order

From Wikipedia, the free encyclopedia
The ordering of strings in alphabetical order with numeric value consideration

In computing,natural sort order (ornatural sorting) is a way of ordering strings that treats embedded numbers as whole numerical values rather than sequences of characters.

While standardalphabetical order compares strings character-by-character (where "10" sorts before "2" because "1" is less than "2"), natural sort order orders them by magnitude of the number, placing "2" before "10".

Natural sort order is designed to address the shortcoming of standardlexicographical order, which often produces counter-intuitive results for humans when dealing with numbered lists, filenames, or version numbers.[1]

NaturalLexicographical
1.jpg

2.jpg

3.jpg

9.jpg

10.jpg

11.jpg

1.jpg

10.jpg

11.jpg

2.jpg

3.jpg

9.jpg

Problem with standard sorting

[edit]

In standard alphabetical (lexicographical) sorting, strings are compared character by character from left to right. This causes numbers to be sorted based on the value of their first digit, rather than their whole numerical value.

For example, a computer using standard sorting will place the string "11" before "2". This occurs because the character "1" (the first digit of 11) has a lower code value than "2". While mathematically correct in terms of character codes, this ordering disrupts the logical sequence expected by users, particularly in file management and data lists.

Operation

[edit]

Natural sorting algorithms generally operate by splitting strings into "chunks" of text and numbers.

  • Text chunks are compared alphabetically (often case-insensitively).
  • Numeric chunks are parsed into integer values and compared numerically.

Comparison of algorithms

[edit]
Standard Sorting (Lexicographical)Natural Sorting
file1.txt

file10.txt

file11.txt

file12.txt

file2.txt

file20.txt

file3.txt

file1.txt

file2.txt

file3.txt

file10.txt

file11.txt

file12.txt

file20.txt

Handling edge cases

[edit]

Different implementations of natural sort may handle edge cases differently:

  • Leading zeros: Some algorithms treat "01" and "1" as identical, while others may enforce an ordering where "01" follows "1" (or vice versa) to ensure a deterministic sort.
  • Whitespace: Most implementations ignore leading or trailing whitespace around the numbers to prevent sorting anomalies.
  • Decimals and Version Numbers: A variation of natural sort, often calledversion sort, is designed to handle multiple numeric segments separated by dots (e.g.,1.2.10 vs1.2.2). In standard sort,1.2.10 precedes1.2.2; in version sort, the segments are parsed individually, correctly placing1.2.10 after1.2.2.

History and implementations

[edit]

Functionality to sort by natural sort order is now widely available in software libraries for many programming languages and operating systems.

The concept gained significant visibility in the Macintosh community. During the 1996MacHack conference, the Natural OrderMac OSSystem Extension was conceived and implemented overnight as an entry for the Best Hack contest.[2][3] Subsequently, Dave Koelle published the "Alphanum Algorithm" in 1997,[4] a popular reference implementation that influenced many later libraries. Martin Pool published "Natural Order String Comparison" in 2000.[5]

Modern implementations include:

  • PHP: Thenatsort() function is built into the standard library.[6]
  • Python: Thenatsort library is a widely used third-party package.[7]
  • Perl: The moduleSort::Naturally is available via CPAN.[8]
  • Unix/Linux: The GNUls andsort commands support natural sorting via the-v (version sort) flag.[9]
  • .NET/C#: Various extensions exist, such asNaturalSort.Extension.[10]

File managers such as Windows Explorer (since Windows XP) andMidnight Commander utilize natural sorting by default to display file lists.

See also

[edit]

References

[edit]
  1. ^"Sorting for Humans : Natural Sort Order".blog.codinghorror.com. 12 December 2007.
  2. ^"Natural Order Numerical Sorting".
  3. ^"TidBITS: The Natural Order of Things". 3 February 1997.
  4. ^"Dave Koelle's Alphanum Algorithm".
  5. ^"Martin Pool's Natural Order String Comparison".
  6. ^"PHP: natsort - Manual".php.net.
  7. ^Morton, Seth M. (23 December 2021)."natsort: Simple yet flexible natural sorting in Python" – via PyPI.
  8. ^"Sort::Naturally - metacpan.org".metacpan.org.
  9. ^"Version sort overview (GNU Coreutils 9.8)".www.gnu.org. Retrieved2025-10-26.
  10. ^Pažourek, Tomáš (1 April 2022)."NaturalSort.Extension: Support for natural sorting in .NET/C#".github.com.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Natural_sort_order&oldid=1327660138"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp