开发者

How to sort array with undefined elements in IE7 JavaScript

I am having trouble sorting an array that includes undefined elements (a sparse array) in IE7. This works great in Safari and Firefox of course, and I haven't tried other versions of IE, but here is a s开发者_JAVA技巧imple example.

<html>
<head>
<script type="text/javascript">
function runscript() {
    var myArray = [{id: 2},
                        undefined,
                        {id: 0},
                        {id: 1},
                        {id: 3},
                        {id: 4},
                        {id: 5}];
    myArray.sort(function compare(a, b) { return a.id - b.id; });
    var output = '';
    for (loop in myArray) {
        output += myArray[loop].id + ' ';
    }
    alert(output);
}
</script>
</head>
<body onLoad="runscript();">
</body>

The alert() at the end inexplicably shows 0 2 3 4 5 1. Removing the undefined element from the array correctly sorts it and the alert shows 0 1 2 3 4 5.

Is there a way to work around this in IE7 so that I can reliably sort arrays that include undefined elements? I don't care where the undefined elements end up as long as the defined elements are sorted correctly.


Try changing for (loop in myArray) to for (var loop=0; loop<myArray.length; loop++):

function runscript() {
    var myArray = [{id: 2},
                        undefined,
                        {id: 0},
                        {id: 1},
                        {id: 3},
                        {id: 4},
                        {id: 5}];
    myArray.sort(function compare(a, b) { return a.id - b.id; });
    var output = '';
    for (var loop=0; loop<myArray.length; loop++) {
        output += (myArray[loop]||{id: 'undefined'}).id + ' ';
    }
    alert(output);
}
runscript()

when using the for (x in object) the items aren't guaranteed to be in in order. See also Why is using "for...in" with array iteration a bad idea?

(The above alerts 0 1 2 3 4 5 undefined)

EDIT: Undeleted - I've tested the above and it works :-P


Maybe you can modify your comparator

myArray.sort(function compare(a, b) { return a.id || 0 - b.id || 0; });


First of all, your sort function is wrong as it is expected to return -1, 0 or +1, not a boolean value.
Use this instead

var arr = [.....]
arr.sort((function(a, b){
    if (!a || !b) {
        // Here you choose how to treat undefined/null elements
        return 0;
    }  
    return (a[index].id === b[index].id ? 0 : (a[index].id < b[index].id ? -1 : 1));
})

But just so that you know, that loop of yours is going to throw an error when trying to return the id property from the undefined element. Also, you should never use a for..in loop to iterate over an array, use either a loop with incrementing index or a reverse while like this

var l = arr.length; 
while (l--) {
    ..
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜