Question

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 simple 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.

Was it helpful?

Solution

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

OTHER TIPS

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--) {
    ..
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top