Bubble sort dropped images based on their names

Posted by user2259784 on Stack Overflow See other posts from Stack Overflow or by user2259784
Published on 2013-06-28T21:48:59Z Indexed on 2013/06/28 22:21 UTC
Read the original article Hit count: 257

I have written a simple bubble sort function that sort an array of Image objects based on image names. For some reason my function is not swapping the elements of array when needed ( basically new assignment is not working)

Here is my code :

        listOfFiles = event.dataTransfer.files;
        sortImages(listOfFiles);
        function sortImages(listOfFiles)
        {
            var re = /[0-9]/;
            var temp;
            for( var index=0; index < listOfFiles.length ; index++)
            {
                for ( var index2=0; index2 < listOfFiles.length-1 ; index2++)
                {
                    var one = parseFloat(re.exec(listOfFiles[index2].name ));
                    var two = parseFloat(re.exec(listOfFiles[index2+1].name));
                    console.log(one + " : " + two);
                    if (one > two) 
                    {
                        console.log(listOfFiles[index2+1]);
                        console.log(listOfFiles[index2]);
                        //following three lines don't work
                        temp = listOfFiles[index2+1];
                        listOfFiles[index2+1] = listOfFiles[index2];
                        listOfFiles[index2] = temp;


                        console.log(listOfFiles[index2+1]);
                        console.log(listOfFiles[index2]);
                    }
                }
            }
        }

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about arrays