admin管理员组文章数量:1193778
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
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
Share Improve this question edited Apr 20, 2018 at 19:05 user4157124 2,90414 gold badges30 silver badges44 bronze badges asked Mar 21, 2015 at 7:25 OragonOragon 1751 gold badge1 silver badge7 bronze badges 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? – Oragon Commented Mar 23, 2015 at 21:09
3 Answers
Reset to default 20var 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
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.
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])
}
}
}
本文标签: sortingcan javascript sort strings containing numbersStack Overflow
版权声明:本文标题:sorting - can javascript sort strings containing numbers? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738470250a2088542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论