View Single Post
11/12/14, 07:36 PM   #2
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by merlight View Post
ZO_ComboBox allows custom ordering of items via ZO_ComboBox:SetSortOrder(sortOrder, sortType)
I can attach custom data to entries, and use them for sorting, but currently only as tiebreakers. The limitation lies in function ComboBoxSortHelper, where "name" is hardcoded as the primary sort key.

I have a combo box with 1 "default" item and a variable number of user-created items. The "default" item shall always be on top of the drop-down list, while the rest should be properly sorted (they can be added/removed at run-time). So I added "priority" key to each entry, the "default" entry has priority=1, user entries have priority=2. Now I want to set "priority" as the primary sort key, and "name" as the tiebreaker, like this:
Lua Code:
  1. combo:SetSortOrder(ZO_SORT_ORDER_UP, {priority={tiebreaker="name"}, name={}})
  2. combo:SetPrimarySortKey("priority") -- or a third parameter to the previous function, doesn't matter much
This one may or may not be possible, but if your only populating the box once, you could add everything except the top item, sort it & then insert the top item in key = 1 position:
Lua Code:
  1. table.insert(comboBox.m_sortedItems, 1, {name = "top of the list", control ="whatever", ...})

Or probably a better idea would be to just sort the table yourself...or partial do it yourself. Call table.sort & call your own sort function to check if one of the items is the item you want at the top of the list & return the appropriate value, else pass it on to the ZO_TableOrderingFunction to sort by name.
Lua Code:
  1. local function MySort(item1, item2)
  2.     if item1.name == "top of the list" then
  3.         return true
  4.     elseif item2.name == "top of the list" then
  5.         return false
  6.     end
  7.     return ZO_TableOrderingFunction(item1, item2, "name", comboBox.m_sortType, comboBox.m_sortOrder)
  8. end
  9.    
  10. table.sort(comboBox.m_sortedItems, function(item1, item2) return MySort(item1, item2) end)

Oh, and if your storing other data in there besides name and were wanting to sort by that, you could still do the same thing just alter it for whatever data you wanted to sort by. You could pass whatever key you want to ZO_TableOrderingFunction, or just do the entire sort yourself in your own MySort() function.
You just may also need to manually set (if they need changed):
Lua Code:
  1. comboBox.m_sortOrder = sortOrder or ZO_SORT_ORDER_UP
  2. comboBox.m_sortType = sortType or ZO_SORT_BY_NAME

Last edited by circonian : 11/12/14 at 07:42 PM.