Sort an array except one element in JavaScript

前端 未结 10 2519
感动是毒
感动是毒 2021-02-12 01:31

I have an array and I am sorting it but I need to sort everything except one element of my array.

My array is:

var Comparison = [
    {key: \"None\", val         


        
10条回答
  •  感情败类
    2021-02-12 01:59

    var Comparison_sort = this.Comparison.sort(function (a, b) {
      if(a.key == b.key) return 0;
      if (a.key == 'None') return -1;
      if (b.key == 'None') return 1;
    
      if (a.key < b.key)
          return -1;
      if (a.key > b.key)
          return 1;
      return 0;
    });
    

    tells "do a regular sort, except if the key is none which means it must go first."

提交回复
热议问题