最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Array Sort not working - Stack Overflow

programmeradmin0浏览0评论

I have an array of objects that I am trying to sort but it does not seem to be working. Some objects in the array have a orderNum property which I am targeting to sort. But not all objects have this property.

I want the objects with the orderNum property to be sorted to the top positions in the array.

Here is a fiddle to what i have tried: /

Here is my javascript:

var data = {
  "attributes": [
    {
        "value": "123-456-7890",
        "name": "phone"
    },
    {
        "value": "[email protected]",
        "name": "email"
    },
    {
        "value": "Gotham",
        "name": "city",
        "orderNum": 1
    },
    {
        "value": "12",
        "name": "ID"
    },
    {
        "value": "Batman",
        "name": "Super Hero",
        "orderNum": 2
    }
  ]
};

data.attributes.sort( function (a, b) {
  if (a.orderNum < b.orderNum) {
    return -1;
  }
  if (a.orderNum > b.orderNum) {
    return 1;
  }

  return 0;
});

console.log(data);

I have an array of objects that I am trying to sort but it does not seem to be working. Some objects in the array have a orderNum property which I am targeting to sort. But not all objects have this property.

I want the objects with the orderNum property to be sorted to the top positions in the array.

Here is a fiddle to what i have tried: http://jsfiddle.net/7D8sN/

Here is my javascript:

var data = {
  "attributes": [
    {
        "value": "123-456-7890",
        "name": "phone"
    },
    {
        "value": "[email protected]",
        "name": "email"
    },
    {
        "value": "Gotham",
        "name": "city",
        "orderNum": 1
    },
    {
        "value": "12",
        "name": "ID"
    },
    {
        "value": "Batman",
        "name": "Super Hero",
        "orderNum": 2
    }
  ]
};

data.attributes.sort( function (a, b) {
  if (a.orderNum < b.orderNum) {
    return -1;
  }
  if (a.orderNum > b.orderNum) {
    return 1;
  }

  return 0;
});

console.log(data);
Share Improve this question edited Apr 25, 2014 at 18:49 Mdd asked Apr 25, 2014 at 18:41 MddMdd 4,40012 gold badges47 silver badges71 bronze badges 2
  • 2 Works for me. You really should have a value for orderNum for the undefined ones. Your getting odd results because you are comparing undefined and your sort function is returning 0 when you compare ones with values to ones without values. – jeremy Commented Apr 25, 2014 at 18:48
  • 1 @jeremy That's the whole point of his question, he wants to know how to avoid getting odd results for elements with no orderNum. – Barmar Commented Apr 25, 2014 at 18:56
Add a comment  | 

4 Answers 4

Reset to default 9

Check if the property exists in your sort function.

data.attributes.sort( function (a, b) {
    if ((typeof b.orderNum === 'undefined' && typeof a.orderNum !== 'undefined') || a.orderNum < b.orderNum) {
        return -1;
    }
    if ((typeof a.orderNum === 'undefined' && typeof b.orderNum !== 'undefined') || a.orderNum > b.orderNum) {
        return 1;
    }

    return 0;
});

You have to check specifically for the property being undefined. Otherwise, both tests return false, so you fall through to return 0 and treat them as equal to everything.

data.attributes.sort( function (a, b) {
  if (a.orderNum === undefined || a.orderNum < b.orderNum) {
    return -1;
  }
  if (b.orderNum === undefined || b.orderNum < a.orderNum) {
    return 1;
  }

  return 0;
});

You can check whether each object has the property with hasOwnProperty("orderNum") and then sort them accordingly. If one has it, and the other does not, then the one with it gets put first. I made the assumption that you were sorting with orderNum ascending.

JSFiddle: http://jsfiddle.net/dshell/RFr5N/

data.attributes.sort( function (a, b) {
    if ((a.hasOwnProperty("orderNum")) && (b.hasOwnProperty("orderNum")))
    {
        return a.orderNum - b.orderNum;
    }
    else if (a.hasOwnProperty("orderNum"))
    {
        return -1;
    }
    else if (b.hasOwnProperty("orderNum"))
    {
        return 1;
    }

    return 0;
});

What you need is to 'normalize' your input :

data.attributes.sort( function (a, b) {
   var aOrderNum = ( a.orderNum === undefined ) ? -1 : a.orderNum ;
   var bOrderNum = ( b.orderNum === undefined ) ? -1 : b.orderNum ;
   return aOrderNum - bOderNum;
 });
发布评论

评论列表(0)

  1. 暂无评论