Module:HeroData/nav: Difference between revisions

LVL (talk | contribs)
Improved previous change to hero sorting. Now uses the wiki's intended _sort key data structure
LVL (talk | contribs)
Fixed the comments
Line 50: Line 50:
                 end
                 end
                  
                  
                 -- ===== FINAL, CORRECT LOGIC START =====
                 -- Create a sort key for each hero to ensure correct alphabetical ordering.
                -- This logic handles special cases like "The Doorman" by looking for a "_sort" version of the hero's key in the language files.
                 local hero_name = lang_module.get_string(hero_key, nil, 'en')
                 local hero_name = lang_module.get_string(hero_key, nil, 'en')
                 local sort_key_to_find = hero_key .. '_sort'
                 local sort_key_to_find = hero_key .. '_sort'
Line 56: Line 57:
                  
                  
                 local sort_key
                 local sort_key
                 -- string.find returns nil if the substring is not found.
                 -- The lang_module returns the requested key (wrapped in HTML) if a translation is not found.
                 -- This is our check to see if the translation module failed and returned the key itself.
                 -- We detect this failure by checking if the returned string contains the key we originally searched for.
                 if string.find(potential_sort_key, sort_key_to_find, 1, true) then
                 if string.find(potential_sort_key, sort_key_to_find, 1, true) then
                     -- Failure case: The returned string contains the key we looked for. Fall back to the display name.
                     -- If the key is found in the result, it means the translation failed. Fall back to the hero's display name for sorting.
                     sort_key = hero_name
                     sort_key = hero_name
                 else
                 else
                     -- Success case: The returned string is a valid translation (like "Doorman"). Use it.
                     -- If the key is NOT found, it means we received a valid sort key (e.g., "Doorman"). Use it.
                     sort_key = potential_sort_key
                     sort_key = potential_sort_key
                 end
                 end
Line 71: Line 72:
                               '|recommended=' .. tostring(is_recommended)
                               '|recommended=' .. tostring(is_recommended)
                  
                  
                -- Store the sort key and the wikitext together in a table for sorting.
                 table.insert(heroes, {sort_key = sort_key, wikitext = hero_wikitext})
                 table.insert(heroes, {sort_key = sort_key, wikitext = hero_wikitext})
                -- ===== FINAL, CORRECT LOGIC END =====
             end
             end
         end
         end
     end
     end
          
          
     -- Use a custom sort function
     -- Sort the list of heroes alphabetically based on their assigned sort_key.
     table.sort(heroes, function(a, b)
     table.sort(heroes, function(a, b)
         return a.sort_key < b.sort_key
         return a.sort_key < b.sort_key
     end)
     end)
      
      
     -- Add each item to output
     -- Build the final wikitext output from the now-sorted table.
     local ret = ''
     local ret = ''
     for index, hero_object in ipairs(heroes) do
     for index, hero_object in ipairs(heroes) do