Thread Tools Display Modes
10/11/14, 03:40 PM   #1
Argusus
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 120
ZO_Helpers

I saw in Zgoo that there was a "Zo_BinarySearch" is there anywhere to find more docs on this or example uses?
  Reply With Quote
10/11/14, 04:24 PM   #2
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
I remember when I wanted to use it I read the code and concluded it was broken. So I wrote my own. Well, it might be me having a bad day and reading the code wrong.

But even if it actually worked, it's badly designed. Unlike table.sort, which expects a comparator that takes 2 arguments and returns a boolean (basically operator '<'; you need to have a sorted table in order to use binary search), zo_binarysearch expects a comparator that takes 3 arguments and returns numeric difference (similar to perl's operator '<=>'; the third argument is index, which is totally irrelevant, but they still use it in their default broken comparator instead of the right-hand value). So in the end, you'd need to write two comparators - one for sorting and another for searching - awful design.

Last edited by merlight : 10/11/14 at 04:30 PM.
  Reply With Quote
10/11/14, 04:36 PM   #3
Argusus
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 120
Originally Posted by merlight View Post
I remember when I wanted to use it I read the code and concluded it was broken. So I wrote my own. Well, it might be me having a bad day and reading the code wrong.

But even if it actually worked, it's badly designed. Unlike table.sort, which expects a comparator that takes 2 arguments and returns a boolean (basically operator '<'; you need to have a sorted table in order to use binary search), zo_binarysearch expects a comparator that takes 3 arguments and returns numeric difference (similar to perl's operator '<=>'; the third argument is index, which is totally irrelevant, but they still use it in their default broken comparator instead of the right-hand value). So in the end, you'd need to write two comparators - one for sorting and another for searching - awful design.
Ah, doesn't surprise me... too much mystery in the addon world right now. The post really relates to me wanting to sort the "masterList" alphabetically by a column when the BuildMasterList is called. it doesn't seem to be doing this by default so maybe i'm missing something there. I was thinking I could sort the table THEN add the items to the masterList object so it sorted by my first column (UnitName) by default.
  Reply With Quote
10/11/14, 05:02 PM   #4
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
You don't need to sort anything in BuildMasterList. There's SortScrollList for that, it's called after the list is filtered to sort only items that pass the filter; and also when sort order changes, the list is not rebuilt, just sorted again. I'm looking into SLE and it looks like everything's in place.

Try this in UnitList:Initialize():
Lua Code:
  1. units.sortHeaderGroup:SelectHeaderByKey("name", ZO_SortHeaderGroup.SUPPRESS_CALLBACKS)

edited: wrong member name

Last edited by merlight : 10/11/14 at 05:04 PM.
  Reply With Quote
10/11/14, 05:07 PM   #5
Argusus
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 120
Originally Posted by merlight View Post
You don't need to sort anything in BuildMasterList. There's SortScrollList for that, it's called after the list is filtered to sort only items that pass the filter; and also when sort order changes, the list is not rebuilt, just sorted again. I'm looking into SLE and it looks like everything's in place.

Try this in UnitList:Initialize():
Lua Code:
  1. units.sortHeaderGroup:SelectHeaderByKey("name", ZO_SortHeaderGroup.SUPPRESS_CALLBACKS)

edited: wrong member name
Code:
function UnitList:Initialize()
 	self.masterList = {}
 	ZO_ScrollList_AddDataType(self.list, 1, "UnitRow", 30, function(control, data) self:SetupUnitRow(control, data) end)
 	ZO_ScrollList_EnableHighlight(self.list, "ZO_ThinListHighlight")	
 	self.sortFunction = function(listEntry1, listEntry2) return ZO_TableOrderingFunction(listEntry1.data, listEntry2.data, self.currentSortKey, UnitList.SORT_KEYS, self.currentSortOrder) end
	self:RefreshData()
end
My Current code looks like the attached image, should I put this at the end of the method or where?
Attached Thumbnails
Click image for larger version

Name:	example.png
Views:	596
Size:	11.7 KB
ID:	488  
  Reply With Quote
10/11/14, 05:29 PM   #6
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
I should read more carefully before pasting lines from different places. Now I think it should not suppress the callback, because it sets currentSortKey and currentSortOrder, which are then used in sortFunction.

I'd place it right before RefreshData call, without SUPPRESS_CALLBACK:

Lua Code:
  1. function UnitList:Initialize()
  2.     self.masterList = {}
  3.     ZO_ScrollList_AddDataType(self.list, 1, "UnitRow", 30, function(control, data) self:SetupUnitRow(control, data) end)
  4.     ZO_ScrollList_EnableHighlight(self.list, "ZO_ThinListHighlight")   
  5.     self.sortFunction = function(listEntry1, listEntry2) return ZO_TableOrderingFunction(listEntry1.data, listEntry2.data, self.currentSortKey, UnitList.SORT_KEYS, self.currentSortOrder) end
  6.     self.sortHeaderGroup:SelectHeaderByKey("name")
  7.     self:RefreshData()
  8. end

I'm still wondering how it could work without setting a sort key. I'd expect it to throw a "nil used as table key" error when there are 2 or more items in the list.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » ZO_Helpers


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off