Skip to content

Commit af9ca01

Browse files
committed
better sort
1 parent 0d63f08 commit af9ca01

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

src/Indexer.hx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
3131
<!-- End Google Tag Manager (noscript) -->';
3232
}
3333

34+
static function getDateStr(v:String):Null<String> {
35+
final dateRegex = ~/[0-9]{4}-[0-9]{2}-[0-9]{2}/;
36+
return dateRegex.match(v) ? dateRegex.matched(0) : null;
37+
}
38+
3439
public static function buildIndexPage(dirs:Array<String>, records:Array<Record>):String {
3540
final maxSizes = { date:25, size:15, fname:0 };
3641
for (r in records)
@@ -42,7 +47,20 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
4247
if (r.fname.length > maxSizes.fname)
4348
maxSizes.fname = r.fname.length;
4449
}
45-
records.sort(function(v1,v2) return Reflect.compare(v2.fname,v1.fname));
50+
// sort files
51+
// 1. by date in filename (newest first)
52+
// 2. by last modified date (newest first) if date in filename is the same
53+
// 3. by filename if no date in filename
54+
records.sort(function(v1,v2) {
55+
return switch [getDateStr(v1.fname), getDateStr(v2.fname)] {
56+
case [d1, d2] if (d1 != null && d2 != null && d1 != d2):
57+
Reflect.compare(d2, d1);
58+
case [d1, d2] if (d1 != null && d2 != null && d1 == d2):
59+
Reflect.compare(v2.date, v1.date);
60+
case _:
61+
Reflect.compare(v2.fname, v1.fname);
62+
}
63+
});
4664
final buf = new StringBuf();
4765

4866
buf.add(

0 commit comments

Comments
 (0)