[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 @ 2022-02-07 16:39  Zhentiw  阅读(37)  评论(0编辑  收藏  举报