Module:HeroData/nav: Difference between revisionsGive feedback
Fix alphabetical sorting for heroes starting with "The" (e.g., The Doorman). |
Improved previous change to hero sorting. Now uses the wiki's intended _sort key data structure |
||
| Line 38: | Line 38: | ||
for hero_key, hero_data in pairs(heroes_data) do | for hero_key, hero_data in pairs(heroes_data) do | ||
if hero_data["Name"] ~= nil then | if hero_data["Name"] ~= nil then | ||
local this_hero_status = hero_release_module.get_hero_release_status(hero_key) | local this_hero_status = hero_release_module.get_hero_release_status(hero_key) | ||
| Line 52: | Line 50: | ||
end | end | ||
-- ===== | -- ===== FINAL, CORRECT LOGIC START ===== | ||
local hero_name = lang_module.get_string(hero_key, nil, 'en') | local hero_name = lang_module.get_string(hero_key, nil, 'en') | ||
local | local sort_key_to_find = hero_key .. '_sort' | ||
local potential_sort_key = lang_module.get_string(sort_key_to_find, nil, 'en') | |||
-- | local sort_key | ||
if string. | -- string.find returns nil if the substring is not found. | ||
-- This is our check to see if the translation module failed and returned the key itself. | |||
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. | |||
sort_key = hero_name | |||
else | |||
-- Success case: The returned string is a valid translation (like "Doorman"). Use it. | |||
sort_key = potential_sort_key | |||
end | end | ||
| Line 67: | Line 72: | ||
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 | ||
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 | ||
| Line 78: | Line 84: | ||
-- Add each item to output | -- Add each item to output | ||
local ret = '' | local ret = '' | ||
for index, hero_object in ipairs(heroes) do | for index, hero_object in ipairs(heroes) do | ||
ret = ret .. '{{Hero card2' .. hero_object.wikitext | ret = ret .. '{{Hero card2' .. hero_object.wikitext | ||