How do you sort an array of strings naturally in different programming languages? Post your implementation and what language it is in in the answer.
In computing, natural sort order (or natural sorting) is the ordering of strings in alphabetical order, except that multi-digit numbers are treated atomically, i.e., as if they were a single character.
The natsort() function sorts an array by using a "natural order" algorithm. The values keep their original keys. In a natural algorithm, the number 2 is less than the number 10.
Sorting in Lists The content of lists in Java are sorted according to natural order. This means that, if the content are numbers, it is sorted as ascending or lowest to highest order when sorted. Similarly, characters are sorted according to their alphabetical order.
ASCII Order The main features of the ASCII sequence are that digits are sorted before uppercase letters, and uppercase letters are sorted before lowercase letters. The blank is the smallest displayable character.
Here's how you can get explorer-like behaviour in Python:
#!/usr/bin/env python """ >>> items = u'a1 a003 b2 a2 a10 1 10 20 2 c100'.split() >>> items.sort(explorer_cmp) >>> for s in items: ... print s, 1 2 10 20 a1 a2 a003 a10 b2 c100 >>> items.sort(key=natural_key, reverse=True) >>> for s in items: ... print s, c100 b2 a10 a003 a2 a1 20 10 2 1 """ import re def natural_key(astr): """See http://www.codinghorror.com/blog/archives/001018.html""" return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', astr)] def natural_cmp(a, b): return cmp(natural_key(a), natural_key(b)) try: # use explorer's comparison function if available import ctypes explorer_cmp = ctypes.windll.shlwapi.StrCmpLogicalW except (ImportError, AttributeError): # not on Windows or old python version explorer_cmp = natural_cmp if __name__ == '__main__': import doctest; doctest.testmod()
To support Unicode strings, .isdecimal()
should be used instead of .isdigit()
.
.isdigit()
may also fail (return value that is not accepted by int()
) for a bytestring on Python 2 in some locales e.g., '\xb2' ('²') in cp1252 locale on Windows.
JavaScript
Array.prototype.alphanumSort = function(caseInsensitive) { for (var z = 0, t; t = this[z]; z++) { this[z] = [], x = 0, y = -1, n = 0, i, j; while (i = (j = t.charAt(x++)).charCodeAt(0)) { var m = (i == 46 || (i >=48 && i <= 57)); if (m !== n) { this[z][++y] = ""; n = m; } this[z][y] += j; } } this.sort(function(a, b) { for (var x = 0, aa, bb; (aa = a[x]) && (bb = b[x]); x++) { if (caseInsensitive) { aa = aa.toLowerCase(); bb = bb.toLowerCase(); } if (aa !== bb) { var c = Number(aa), d = Number(bb); if (c == aa && d == bb) { return c - d; } else return (aa > bb) ? 1 : -1; } } return a.length - b.length; }); for (var z = 0; z < this.length; z++) this[z] = this[z].join(""); }
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With