How to get sorted items in tlistview in delphi? 
A few days ago we asked the question. The answers are ok but ihey solved half.
There is a listview with about 10000 items that has been sorted (for binary search):
<pre>
aa cd
aa ef
..
ab cd // want to get items from here
ab ef
ab gh
ab ij
ab kl
ab mn //to here
ac cd
ac ef
..
ak cd
ak ef
ak gh
..

We should get items starting with ab. First we use binary search and find one of items starting with ab then we use linear search to try to get all ab items just by starting from  the index that binary search found. Is this correct? 
But it does not work. Please help. Thank you in advance.


function TForm1.binsearch(lv: tlistview; st: string): integer;
var
  L,R,M: Integer;
  p:integer;
  cap, wholecap:string;
  CompareResult: Integer;
  alist, newlist:tlistitem;
  fresult:integer;
  newresult:integer;
  found: boolean;
begin
  Result := -1;
  cap:='';
  wholecap:='';
  L := 0;
  R := lv.items.Count-1;
  M := (L + R) div 2;  
  wholecap:=lv.Items[m].caption;
  p:=pos(' ',wholecap);
  cap:=trim(copy(wholecap, 0, p));
  CompareResult := Comparestr(cap, st);
  while (compareresult<>0) and (l<=r) do begin
  if CompareResult > 0 then begin
  R := M - 1;
  end;
  if CompareResult < 0
  then begin
  L := M + 1;
  end;
  M := (L + R) div 2;
  wholecap:=lv.Items[m].caption;   
  if pos(' ', wholecap)>0 then
  p:=pos(' ',wholecap);
  cap:=trim(copy(wholecap, 0, p));
  CompareResult := Comparestr(cap, st);
  end;

fresult:=m;
result:=m;
newresult:=m;

// Above is ok, we can find that item starting with st(here, e.g. ab), 
// But next is when we want to find items near ab and also starting with ab,
// it seems it does not work.
// hmmtemplistview is another listview.  

alist:=lv.Items[fresult];
wholecap:=alist.caption;
if pos(' ', wholecap)>0 then
p:=pos(' ',wholecap);
cap:=trim(copy(wholecap, 0, p));
if cap=st then
found:=true;

while  (fresult>=0) and found=true do begin
newlist:=hmmtemplistview.Items.Insert(0);
newlist.Caption :=wholecap;
hmmtemplistview.items.Item[0]:=alist;

dec(fresult);
if fresult<0 then begin
break;
end;
alist:=lv.Items[fresult];
wholecap:=alist.caption;
p:=pos(' ',wholecap);
cap:=trim(copy(wholecap, 0, p));
if cap<>st then
begin
found:=false;
end;
end;


if result<>-1 then
newresult:=result+1;
alist:=lv.Items[newresult];
wholecap:=alist.caption;
if pos(' ', wholecap)>0 then
p:=pos(' ',wholecap);
cap:=trim(copy(wholecap, 0, p));
if cap=st then
found:=true;
while  (newresult>=0) and found=true do begin
newlist:=hmmtemplistview.Items.Insert(0);
newlist.Caption :=wholecap;
hmmtemplistview.items.Item[0]:=alist;
inc(newresult);
if newresult>lv.Items.Count then begin
break;
end;
alist:=lv.Items[newresult];
wholecap:=alist.caption;
p:=pos(' ',wholecap);
cap:=trim(copy(wholecap, 0, p));
if cap<>st then
begin
found:=false;
end;
end;
end;

<code>

Please help. Thank you in advance.
How to get sorted items in tlistview in delphi? 
===

A few days ago we asked the question. The answers are ok but ihey solved half.
There is a listview with about 10000 items that has been sorted (for binary search):
<pre>
aa cd
aa ef
..
ab cd // want to get items from here
ab ef
ab gh
ab ij
ab kl
ab mn //to here
ac cd
ac ef
..
ak cd
ak ef
ak gh
..

We should get items starting with ab. First we use binary search and find one of items starting with ab then we use linear search to try to get all ab items just by starting from  the index that binary search found. Is this correct? 
But it does not work. Please help. Thank you in advance.


function TForm1.binsearch(lv: tlistview; st: string): integer;
var
  L,R,M: Integer;
  p:integer;
  cap, wholecap:string;
  CompareResult: Integer;
  alist, newlist:tlistitem;
  fresult:integer;
  newresult:integer;
  found: boolean;
begin
  Result := -1;
  cap:='';
  wholecap:='';
  L := 0;
  R := lv.items.Count-1;
  M := (L + R) div 2;  
  wholecap:=lv.Items[m].caption;
  p:=pos(' ',wholecap);
  cap:=trim(copy(wholecap, 0, p));
  CompareResult := Comparestr(cap, st);
  while (compareresult<>0) and (l<=r) do begin
  if CompareResult > 0 then begin
  R := M - 1;
  end;
  if CompareResult < 0
  then begin
  L := M + 1;
  end;
  M := (L + R) div 2;
  wholecap:=lv.Items[m].caption;   
  if pos(' ', wholecap)>0 then
  p:=pos(' ',wholecap);
  cap:=trim(copy(wholecap, 0, p));
  CompareResult := Comparestr(cap, st);
  end;

fresult:=m;
result:=m;
newresult:=m;

// Above is ok, we can find that item starting with st(here, e.g. ab), 
// But next is when we want to find items near ab and also starting with ab,
// it seems it does not work.
// hmmtemplistview is another listview.  

alist:=lv.Items[fresult];
wholecap:=alist.caption;
if pos(' ', wholecap)>0 then
p:=pos(' ',wholecap);
cap:=trim(copy(wholecap, 0, p));
if cap=st then
found:=true;

while  (fresult>=0) and found=true do begin
newlist:=hmmtemplistview.Items.Insert(0);
newlist.Caption :=wholecap;
hmmtemplistview.items.Item[0]:=alist;

dec(fresult);
if fresult<0 then begin
break;
end;
alist:=lv.Items[fresult];
wholecap:=alist.caption;
p:=pos(' ',wholecap);
cap:=trim(copy(wholecap, 0, p));
if cap<>st then
begin
found:=false;
end;
end;


if result<>-1 then
newresult:=result+1;
alist:=lv.Items[newresult];
wholecap:=alist.caption;
if pos(' ', wholecap)>0 then
p:=pos(' ',wholecap);
cap:=trim(copy(wholecap, 0, p));
if cap=st then
found:=true;
while  (newresult>=0) and found=true do begin
newlist:=hmmtemplistview.Items.Insert(0);
newlist.Caption :=wholecap;
hmmtemplistview.items.Item[0]:=alist;
inc(newresult);
if newresult>lv.Items.Count then begin
break;
end;
alist:=lv.Items[newresult];
wholecap:=alist.caption;
p:=pos(' ',wholecap);
cap:=trim(copy(wholecap, 0, p));
if cap<>st then
begin
found:=false;
end;
end;
end;

<code>

Please help. Thank you in advance.