自然排序算法

您如何自然地以不同的编程语言对字符串数组进行排序?在答案中发布您的实现及其使用的语言。


慕的地6264312
浏览 587回答 3
3回答

ibeautiful

的JavaScriptArray.prototype.alphanumSort = function(caseInsensitive) {&nbsp; for (var z = 0, t; t = this[z]; z++) {&nbsp; &nbsp; this[z] = [], x = 0, y = -1, n = 0, i, j;&nbsp; &nbsp; while (i = (j = t.charAt(x++)).charCodeAt(0)) {&nbsp; &nbsp; &nbsp; var m = (i == 46 || (i >=48 && i <= 57));&nbsp; &nbsp; &nbsp; if (m !== n) {&nbsp; &nbsp; &nbsp; &nbsp; this[z][++y] = "";&nbsp; &nbsp; &nbsp; &nbsp; n = m;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; this[z][y] += j;&nbsp; &nbsp; }&nbsp; }&nbsp; this.sort(function(a, b) {&nbsp; &nbsp; for (var x = 0, aa, bb; (aa = a[x]) && (bb = b[x]); x++) {&nbsp; &nbsp; &nbsp; if (caseInsensitive) {&nbsp; &nbsp; &nbsp; &nbsp; aa = aa.toLowerCase();&nbsp; &nbsp; &nbsp; &nbsp; bb = bb.toLowerCase();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (aa !== bb) {&nbsp; &nbsp; &nbsp; &nbsp; var c = Number(aa), d = Number(bb);&nbsp; &nbsp; &nbsp; &nbsp; if (c == aa && d == bb) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return c - d;&nbsp; &nbsp; &nbsp; &nbsp; } else return (aa > bb) ? 1 : -1;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return a.length - b.length;&nbsp; });&nbsp; for (var z = 0; z < this.length; z++)&nbsp; &nbsp; this[z] = this[z].join("");}

UYOU

下面是清理代码中的文章链接到的问题:def sorted_nicely(strings):&nbsp;&nbsp; &nbsp; "Sort strings the way humans are said to expect."&nbsp; &nbsp; return sorted(strings, key=natural_sort_key)def natural_sort_key(key):&nbsp; &nbsp; import re&nbsp; &nbsp; return [int(t) if t.isdigit() else t for t in re.split(r'(\d+)', key)]但是实际上我还没有机会用这种方式进行任何排序。
打开App,查看更多内容
随时随地看视频慕课网APP