JavaScript 比较字符串:中文和数字混合排序
function compare(a, b) {/n var pattern = /([/u4e00-/u9fa5]+)(/d+)/;/n/n var aMatch = a.location ? a.location.match(pattern) : null;/n var bMatch = b.location ? b.location.match(pattern) : null;/n/n if (aMatch === null && bMatch === null) {/n return a.location.localeCompare(b.location);/n } else if (aMatch === null) {/n return -1;/n } else if (bMatch === null) {/n return 1;/n } else {/n if (aMatch[1] < bMatch[1]) {/n return -1;/n } else if (aMatch[1] > bMatch[1]) {/n return 1;/n } else {/n return parseInt(aMatch[2]) - parseInt(bMatch[2]);/n } /n }/n}/n
原文地址: https://www.cveoy.top/t/topic/ehRm 著作权归作者所有。请勿转载和采集!