[Javascript] Array sort - keep one item always last

For example, we have a dropdown list, we want to keep "other" option always as last option.

         return options
                .sort((a, b) => {
                  if (a.value == b.value) return 0;
                  if (a.value == 'other') return 1;
                  if (b.value == 'other') return -1;
                  return a.value.localeCompare(b.value);
                });            

 

 

posted @   Zhentiw  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-02-07 [Algorithm] 189. Rotate Array
2020-02-07 [Angular] Preparing an Angular Application for Continuous Integration (CI)
2019-02-07 [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers
2019-02-07 [Angular] Introduction to Angular Internationalization (i18n)
2019-02-07 [Javascript] Wrap an API with a Proxy
2019-02-07 [React] Detect user activity with a custom useIdle React Hook
2019-02-07 [Javascript] Automate the process of flattening deeply nested arrays using ES2019's flat method
点击右上角即可分享
微信分享提示