const a = [“你好”, “哈哈”, “是的”, “阿萨大”];
const b = “阿萨大,哈哈”;
// 将字符串b分割成数组
const bArray = b.split(“,”);
// 使用字符串b对数组a进行匹配排序
const sortedArray = a.sort((x, y) => {
// 获取字符串x和y在bArray中的索引
const xIndex = bArray.indexOf(x);
const yIndex = bArray.indexOf(y);
// 根据索引进行排序
if (xIndex > yIndex) {
return 1;
} else if (xIndex < yIndex) {
return -1;
} else {
return 0;
}
});
// 将排序后的数组转换为字符串
const sortedString = sortedArray.join(“,”);
// 输出排序后的字符串
console.log(sortedString);