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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.