@@ -225,10 +225,8 @@ <h2>Relevance of Indicators for RS Tiers</h2>
225225 let originalData = [ ] ;
226226
227227 const tierOrder = [ "crucial" , "recommended" , "good to have" , "not relevant" ] ;
228- let currentSort = {
229- column : null ,
230- index : - 1
231- } ;
228+ let activeSortColumn = null ;
229+
232230
233231 async function fetchCSVFile ( csvPath ) {
234232 const overlay = document . getElementById ( "table-loading-overlay" ) ;
@@ -322,39 +320,22 @@ <h2>Relevance of Indicators for RS Tiers</h2>
322320 }
323321
324322 function sortByTier ( column ) {
325- if ( currentSort . column !== column ) {
326- currentSort . column = column ;
327- currentSort . index = - 1 ;
328- }
329-
330- const presentValues = tierOrder . filter ( value =>
331- allData . some ( item =>
332- ( item [ column ] || "" ) . toLowerCase ( ) === value
333- )
334- ) ;
335-
336- if ( presentValues . length === 0 ) {
337- return ;
338- }
339-
340- currentSort . index ++ ;
341-
342- if ( currentSort . index >= presentValues . length ) {
343- currentSort . column = null ;
344- currentSort . index = - 1 ;
323+ if ( activeSortColumn === column ) {
324+ activeSortColumn = null ;
345325 updateTableFromCSV ( originalData ) ;
346326 return ;
347327 }
348328
349- const priority = presentValues [ currentSort . index ] ;
329+ activeSortColumn = column ;
350330
351- const sorted = [ ...allData ] . sort ( ( a , b ) => {
352- const aVal = ( a [ column ] || "" ) . toLowerCase ( ) ;
353- const bVal = ( b [ column ] || "" ) . toLowerCase ( ) ;
331+ const orderIndex = value => {
332+ const normalized = ( value || "" ) . toLowerCase ( ) ;
333+ const idx = tierOrder . indexOf ( normalized ) ;
334+ return idx === - 1 ? tierOrder . length : idx ;
335+ } ;
354336
355- if ( aVal === priority && bVal !== priority ) return - 1 ;
356- if ( bVal === priority && aVal !== priority ) return 1 ;
357- return 0 ;
337+ const sorted = [ ...allData ] . sort ( ( a , b ) => {
338+ return orderIndex ( a [ column ] ) - orderIndex ( b [ column ] ) ;
358339 } ) ;
359340
360341 updateTableFromCSV ( sorted ) ;
0 commit comments