• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

sunshinezjb

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

对包含汉字和字母的对象数组进行升序排列

export function ascendSort(arr, propName, empty) {
  let chineseChars = [],
    chars = [],
    list = [];
  (arr || []).forEach(item => {
    // 判断是否为中文
    if (/^[\u4e00-\u9fa5]*$/.test(item[propName].charAt(0))) {
      chineseChars.push(item); // 姓名首字符为中文的
    } else {
      chars.push(item); // 姓名首字符非中文的(字母,数字)
    }
  });
  chars.sort((a, b) => a[propName].charCodeAt(0) - b[propName].charCodeAt(0));
  chineseChars.sort((a, b) =>
    a[propName].charAt(0).localeCompare(b[propName].charAt(0))
  );
  list = chars.concat(chineseChars); // list为最终想要的数组
  return list;
}

  

posted on 2022-02-18 15:13  sunshinezjb  阅读(81)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3