14

I'm making a little web-app in which I used AHK and javascript. I make AHK a group of images paths to the .js file something like this

var importedFiles = [
 "file:///F:/image1.jpg",
 "file:///F:/image10.jpg",
 "file:///F:/image11.jpg",
 "file:///F:/image2.jpg",
]

and these images should be viewed in the browser the problem is that the sorting method of the two languages is not like the windows sorting.

What I want is javascript sort the file in the variable so that they are like they are viewed in windows like this

var importedFiles = [
 "file:///F:/image1.jpg",
 "file:///F:/image2.jpg",
 "file:///F:/image10.jpg",
 "file:///F:/image11.jpg",
]

BTW : I've searched for the functions posted before but it only works with number only and strings containing number like this case

1
  • another question guys, can I find if a string consist of only number , spaces and dashes(-) or there are some characters in the name? can I do that? Commented Mar 23, 2015 at 21:09

3 Answers 3

19
var importedFiles = [
 "file:///F:/image1.jpg",
 "file:///F:/image10.jpg",
 "file:///F:/image11.jpg",
 "file:///F:/image2.jpg",
]

var customSort = function (a, b) {
    return (Number(a.match(/(\d+)/g)[0]) - Number((b.match(/(\d+)/g)[0])));

}

// use sort() and apply the customSort function
console.log(importedFiles.sort(customSort));

//outputs

[
   "file:///F:/image1.jpg", 
   "file:///F:/image2.jpg", 
   "file:///F:/image10.jpg", 
   "file:///F:/image11.jpg"
]

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot i use you method but add another function to get what I want
What if we have a string without the number?
1

You can use a regex to get the number in the filename (assuming your filenames have a pattern to it like the example you have given at the top) and sort it according to it.

var r = /[A-Za-z\:\/]+([0-9]+)\.jpg/;
importedFiles.sort(function(f1, f2) {
     var match1 = r.exec(f1);
     var num1 = match1[1];

     var match2 = r.exec(f2);
     var num2 = match2[1];

     //now you have the two numbers in num1 and num2
     return parseInt(num1, 10) - parseInt(num2, 10);
});

importedFiles will now contain the sorted array as you mentioned.

Comments

0

You will have to split the string to sort it, using a for loop and the .split() method.

Example JS:

function sortNumber(a,b) {
    return a - b;
}
var arrayToSort = ["Orange1","Orange3","Orange2"];
Numbers=[];
sortedArray = [];
for(var i = 0; i< arrayToSort.length; i++) {
    numberValue = arrayToSort[i].split("Orange")[1]//Returns number in string
    Numbers.push(numberValue).sort(sortNumber)
    //Numbers now contains a sorted list of the numbers
}
for(var i = 0; i < arrayToSort.length; i++) {
    for(var j = 0; j < arrayToSort.length; j++) {
        if(arrayToSort[j].indexOf(Numbers[i]) !== -1) {
            sortedArray.push(arrayToSort[j])
        }
    }
}

1 Comment

your case solving only same set of words before numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.