Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 05-22-2017, 03:02 PM
FreakaZoid2 FreakaZoid2 is offline
Senior Member
 
Join Date: Jul 2009
Posts: 300
Default processing order of stringlist

i am looking for a way to process a stringlist in a different order than first to last or last to first.
lets say i load up a stringlist and it has 1200 items. i would like tot process it in this order:
1, 1200, 2, 1199, 3, 1198, 4, 1197,....
any ideas on how to get that order
  #2  
Old 05-23-2017, 07:51 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,222
Default

This is not sorting, easiest is to create a new stringlist where you add them in the correct order:
Code:
procedure TForm2.Button1Click(Sender: TObject);
var
  input: TStringList;
  output: TStringList;
  i: Integer;
  count: Integer;
  iDiv: Integer;
  iMod: Integer;
begin
  input := TStringList.Create();
  for i := 1 to 11 do
    input.Add('item ' + IntToStr(i));

  output := TStringList.Create();
  count := input.Count;
  iDiv := count div 2;
  iMod := count mod 2;
  for i := 0 to iDiv - 1 do
  begin
    output.Add(input[i]);
    output.Add(input[count - (i + 1)]);
  end;

  if iMod <> 0 then
    output.Add(input[i]);

  Memo1.Text := output.Text;
  output.Free();
  input.Free();
end;
  #3  
Old 05-24-2017, 09:26 AM
RoyMoor RoyMoor is offline
Junior Member
 
Join Date: Oct 2014
Posts: 2
Default

var
I: Integer;
begin
for I:=0 to (MyStringList.Count div 2) - 1 do
begin
MyProcedure(MyStringList[I]);
MyProcedure(MyStringList[MyStringList.Count-1-I]);
end;
end;


But if yourlist has a possible odd count, it's safer to do the following (but you will end up with an empty stringlist).
So make and use a copy in front if you need the list afterwards:

begin
while MyStringList.Count > 0 do
begin
MyProcedure(MyStringList[0]);
MyStringList.Delete(0);
if MyStringList.Count > 0 then
begin
MyProcedure(MyStringList[MyStringList.Count-1]);
MyStringList.Delete(MyStringList.Count-1);
end;
end;
end;
Closed Thread

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:44 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.