I'm rather new to understanding Javascript, Jquery and PHP. At this point I've spent close to 80 hours working on understanding this one fix and I'm hoping to gain some insight. I'm working with this drag and drop image upload functionality and I need to sort the image items within the FileList object alphanumerically base on their local filename to coincide with the organizational system of a photograph database. I understand that the FileList object is read-only, so I'm assuming this is achieved by adding these items into their own array to sort or compare.
Using this code,
files = e.dataTransfer.files;
newFiles = Array.from(files).map(({name}) => name);
newFiles.sort();
console.log(files);
console.log(newFiles);
I was able to plug the file names into their own array, but I'm not sure how to proceed in comparing this new list of items to their original FileList counterpart for sorting. Output. (Notice how the last three items of the FileList object (files) returns unsorted and newFiles, while sorted, only contains the name data.)
I've tried utilizing this .slice method:
files = [].slice.call(files);
But I can't seem to get the code to fire. The images upload like normal without being sorted.
Same goes for this method:
files = [...e.dataTransfer.files].sort();
I've also attempted using MDN's recommended code:
files.sort((a, b) => a.localeCompare(b, navigator.languages[0] || navigator.language, {numeric: true, ignorePunctuation: true}));
but when I attempt to implement this fix, the console returns that LocaleCompare is not a function.
I have also looked into using Lauri Rooden's natural compare lite and Boris Moore's jsrender, but I don't understand how to go about implementing these plugins.
I started writing an iterator to determine if the two arrays could be compared and sorted as such, and I'm honestly just wondering if I'm on the right track?
for (i=0; i < files.length; i++) {
if (files.name[i] === newFiles[i]) {
files.splice(i, 1, newFiles[i]) }
else{i++;
}
I'm having some issues with an internal server error in the PHP, but if the regular code works with the error in place, could this be something that stands in the way of the FileList object being sorted?
Here's a fiddle with the code. The PHP is commented out below HTML so the code doesn't run at all & the Javascript iframe is split into 3 sections separated by comments. I know this post is long and there are lots of outside sources, but I wanted be sure I was thorough in my explanation. If you have any advice for an aspiring web dev (other than to quit ^_^;), I would greatly appreciate it! Thank you in advance! :)