有志者事竟成。

js实现按照首字母排序

<script type="text/javascript">
        let obj = [{name:'CA'},{name:'XA'},{name:'CB'},{name:'XE'},{name:'CC'},{name:'XF'},{name:'CA'}]
        let bb = obj.sort((a,b)=>{a.name.charCodeAt(0)-b.name.charCodeAt(0)})
        let cc = obj.sort((a,b)=>a.name.localeCompare(b.name))
        console.log(bb)
        console.log(cc)
    </script>

 

posted on 2021-09-13 11:24  阿长*长  阅读(624)  评论(0编辑  收藏  举报

导航