气泡根据图像的名称对其进行排序
Bubble sort dropped images based on their names
我编写了一个简单的气泡排序函数,该函数根据图像名称对图像对象数组进行排序。出于某种原因,我的功能不是在需要时交换阵列的元素(基本上新的分配不起作用)
这是我的代码:
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]);
}
}
}
}
问题似乎出在返回数组的re.exec()
上。您需要获取包含匹配字符的第一个索引。请参阅此处的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec.
以下内容应该有效:
listOfFiles = event.dataTransfer.files;
sortImages(listOfFiles);
console.log(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)[0]); //here
var two = parseFloat(re.exec(listOfFiles[index2 + 1].name)[0]); //here
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]);
}
}
}
}
相关文章:
- Javascript排序的图像弹出窗口..可以't单独弹出
- 我该如何使网页上的图像网格可排序
- 抽搐流图像并仅在在线流上排序
- Jquery 表排序不适用于图像
- 如何按维度对图像进行排序
- 具有排序问题的流体动态图像布局
- 使用Stupid表插件对图像进行排序
- 使用radio和jQuery对图像进行排序
- 如何在metro.js中实现图像重新排序
- 如何使jQuery中的图像可排序元素可拖动
- 在上传图像之前,对它们进行排序
- 气泡根据图像的名称对其进行排序
- dataTables如何添加排序图像/图标
- 使用数据库与文件系统进行图像排序
- Dropzone图像重新排序
- 重新排序ul li与图像
- n -repeat track by $index打破图像字段排序
- Dojo DataGrid:不对列中的图像进行排序
- 动态图像排序和裁剪类似于google+相册视图
- 使用Jssor滑块对图像进行排序并显示结果