@@ -12,7 +12,7 @@ const ASYNC_PIPELINE_THRESHOLD = 500;
1212// current sort/filter, used for "Go To Card #" and the #N tag on cards.
1313function refreshIndices ( ) {
1414 if ( ! activeData ) return ;
15- const sorted = activeData . slice ( ) . sort ( ( a , b ) => a . id - b . id ) ;
15+ const sorted = activeData . slice ( ) . sort ( ( a , b ) => ( a . gen_index ?? a . id ) - ( b . gen_index ?? b . id ) ) ;
1616 idToIndexMap = new Map ( sorted . map ( ( item , index ) => [ item . id , index + 1 ] ) ) ;
1717}
1818
@@ -167,8 +167,8 @@ function setSecondarySort(id) {
167167// Unified Comparison Helper
168168function compareItems ( a , b , sortKey ) {
169169 switch ( sortKey ) {
170- case 'newest' : return b . id - a . id ;
171- case 'oldest' : return a . id - b . id ;
170+ case 'newest' : return ( b . gen_index ?? b . id ) - ( a . gen_index ?? a . id ) ;
171+ case 'oldest' : return ( a . gen_index ?? a . id ) - ( b . gen_index ?? b . id ) ;
172172 case 'fastest' : return a . duration - b . duration ;
173173 case 'favorited' : return ( b . favorited ? 1 : 0 ) - ( a . favorited ? 1 : 0 ) ;
174174 case 'cfg' : return a . cfg - b . cfg ;
@@ -199,9 +199,9 @@ function runMultiSort(list) {
199199 res = compareItems ( a , b , currentSecondarySort ) ;
200200 }
201201
202- // 3. Fallback to ID (Stable sort)
202+ // 3. Fallback to gen_index/ ID (Stable sort, backwards-compatible )
203203 if ( res === 0 ) {
204- return a . id - b . id ;
204+ return ( a . gen_index ?? a . id ) - ( b . gen_index ?? b . id ) ;
205205 }
206206 return res ;
207207 } ) ;
@@ -313,10 +313,11 @@ function executePipeline() {
313313 } ) ;
314314
315315 // Apply sorting
316- // Fast path: for oldest/newest with no secondary sort, items from activeData.filter()
317- // are already in ID order — just reverse for newest
316+ // Fast path: for oldest/newest with no secondary sort, activeData is stored newest-first
317+ // (items are inserted/unshifted at position 0), so processedData after filter() is also newest-first.
318+ // For 'oldest' we reverse; for 'newest' it's already correct.
318319 if ( ( currentSort === 'oldest' || currentSort === 'newest' ) && currentSecondarySort === 'none' ) {
319- if ( currentSort === 'newest ' ) {
320+ if ( currentSort === 'oldest ' ) {
320321 processedData . reverse ( ) ;
321322 }
322323 } else {
@@ -402,7 +403,7 @@ function binaryInsert(arr, item) {
402403 cmp = compareItems ( arr [ mid ] , item , currentSecondarySort ) ;
403404 }
404405 if ( cmp === 0 ) {
405- cmp = arr [ mid ] . id - item . id ; // Stable sort fallback
406+ cmp = ( arr [ mid ] . gen_index ?? arr [ mid ] . id ) - ( item . gen_index ?? item . id ) ; // Stable sort fallback
406407 }
407408 if ( cmp <= 0 ) {
408409 low = mid + 1 ;
0 commit comments