使用JavaScript进行字母数字排序,js混合排序字母+数字

let arr = ['A1', 'A10', 'A11', 'A12', 'A13', 'A14', 'A15', 'A16', 'A17', 'A18', 'A19', 'A2', 'A20', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'B1', 'B10', 'B11', 'B12', 'B13', 'B19', 'B2', 'B20']
		const alphaNumericSort = (arr = []) => {
		   arr.sort((a, b) => {
			  const aPart = a.split('-');
			  const bPart = b.split('-');
		      return aPart[0].toLowerCase().localeCompare(bPart[0].toLowerCase()) || aPart[1] - bPart[1];
		   });
		};
		alphaNumericSort(arr);
		console.log(111,arr);

posted @ 2022-05-19 17:16  FancyAnnaYL  阅读(1232)  评论(0编辑  收藏  举报