I'm thinking that I'll have to do a binary-search-and-insert
I think you don't need to get too fancy with the inserting algorithm. I simple linear search should scale well up to at least several thousand items in the listbox — and more items isn't particularly good GUI design anyway... (also, my guess would be that reconstructing the listbox content from scratch for every insert operation isn't necessarily faster (I haven't benchmarked it, though)).
So, for all practical purposes, something like this might do (but you could of course implement a binary search, too, if you like):
... sub addItem { my $item = shift(@data); if(!$item) { $ref->cancel; return }; if(! $seen{$item}) { INSERT: { for my $idx (0 .. $list->size()-1) { if ($list->get($idx) ge $item) { $list->insert($idx, $item); last INSERT; } } $list->insert('end', $item); } } $seen{$item}++; }
Moreover, not erasing the listbox content every time would also save you from having to readjust the current scroll position.
In reply to Re: Sorted Realtime Tk Listbox
by almut
in thread Sorted Realtime Tk Listbox
by cmv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |