Sort an array except one element in JavaScript

前端 未结 10 2550
感动是毒
感动是毒 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 02:10

    Just add a check at the beginning. If it's the none object then move it to the front without performing the checks.

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

提交回复
热议问题