问题描述:

I've a small Problem. I habe in an Array some textlines like them:

[

'20140708_____AtestY.cm5',

'20140711_____Ctestyy.cm5',

'20140711_____Dtestzz.cm5',

'20140711_____SBtestyy.cm5'

]

As you can see they're with a date (declared as numbers - i'll divide them later), then the Filename.

i'am trying to sort them by date and this works only nearly.

with the code arrayFileData.sort().reverse() i have a sort by date, but after that it starts also to sort alphabetic.

it is possible to say, look only the date (Nubers)?

Thanks for help.

网友答案:

You can supply your own comparator function to [].sort so it's possible to only look at the first 8 characters (the date part):

arrayFileData.sort(function(a,b){
   return a.substr(0,8) - b.substr(0,8); // you could swap a and b here
                                         // to get the array sorted in reverse order
})

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

相关阅读:
Top