Skip to content

Latest commit

 

History

History
48 lines (35 loc) · 1.3 KB

remove-prefix-sort.md

File metadata and controls

48 lines (35 loc) · 1.3 KB
标题 标签
removePrefixSort(数组去前缀排序) array,sort(数组,排序)

数组去前缀排序。

  • 使用Array.prototype.sort()方法实现数组排序,在内部排序规则中使用String.prototype.replace()方法去除排序字符串前缀

代码如下:

function removePrefixSort(sortArr) {
  return sortArr.sort((a, b) => (delPrefix(a) > delPrefix(b) ? 1 : -1));
}
function delPrefix(item) {
  return item.replace(/^(xtt|code|js)-/, '');
}

ts代码如下:

调用方式:

removePrefixSort([
  'xtt-sort',
  'code-clear',
  'js-array',
  'node',
  'code-echo',
  'js-symbol',
  'css',
  'xtt-romove'
]);
// ['js-array', 'code-clear', 'css', 'code-echo', 'node', 'xtt-romove', 'xtt-sort', 'js-symbol']

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/remove-prefix-sort.html"></iframe>