File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ function solution ( files ) {
2+ let answerWrap = files . map ( ( file , index ) => ( { file, index } ) ) ;
3+
4+ const compare = ( a , b ) => {
5+ const reg = / ( \D * ) ( [ 0 - 9 ] * ) / i;
6+ const A = a . match ( reg ) ;
7+ const B = b . match ( reg ) ;
8+
9+ const compareHead = A [ 1 ] . toLowerCase ( ) . localeCompare ( B [ 1 ] . toLowerCase ( ) ) ;
10+ const compareNumber = ( a , b ) => {
11+ return parseInt ( a ) > parseInt ( b ) ? 1 : parseInt ( a ) < parseInt ( b ) ? - 1 : 0 ;
12+ } ;
13+ return compareHead === 0 ? compareNumber ( A [ 2 ] , B [ 2 ] ) : compareHead ;
14+ } ;
15+
16+ answerWrap . sort ( ( a , b ) => {
17+ const result = compare ( a . file , b . file ) ;
18+ return result === 0 ? a . index - b . index : result ;
19+ } ) ;
20+
21+ return answerWrap . map ( ( answer ) => answer . file ) ;
22+ }
23+
24+ console . log (
25+ solution ( [
26+ 'img12.png' ,
27+ 'img10.png' ,
28+ 'img02.png' ,
29+ 'img1.png' ,
30+ 'IMG01.GIF' ,
31+ 'img2.JPG' ,
32+ ] )
33+ ) ;
You can’t perform that action at this time.
0 commit comments