Module:HeroData/nav: Difference between revisionsGive feedback
Improved previous change to hero sorting. Now uses the wiki's intended _sort key data structure |
Fixed the comments |
||
| Line 50: | Line 50: | ||
end | end | ||
-- | -- 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 | ||
-- | -- The lang_module returns the requested key (wrapped in HTML) if a translation is not found. | ||
-- | -- 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 | ||
-- | -- 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 | ||
-- | -- 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}) | ||
end | end | ||
end | end | ||
end | end | ||
-- | -- 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) | ||
-- | -- 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 | ||