SRL Archive - OSR Help

Alphabetize TStringArray


gerauchert

2013-05-21 10:06:03

I am not too familiar with text functions, so I was wondering if anyone had some ideas of how to code an alphabetizing function to re-order a TStringArray.

This script I am writing is basically a GUI organizer for my mom's personal training clients, and I want it to be able to reorganize the list when a new client is added or one is deleted.

Thanks in advance!

Flight

2013-05-21 10:16:56

I think this is what you're looking for mate:
http://villavu.com/forum/showthread.php?t=82205&p=1224790#post1224790

Janilabo

2013-05-21 11:30:50

@gerauchert

Here is some raw ways to do this (using Simba code, without plugin) - links @pastebin:
ShellSort
HeapSort
QuickSort
QuickSort2
3-Way QuickSort
MergeSort
Bottom-Up MergeSort
InsertionSort
SelectionSort
BubbleSort
TSAJnlbSortDnmc
TSAJnlbSort

Took those algorithms directly from libsorting.dll, edited em a bit and then included small example for each one. :p

Also, if you want it to be really quick, then I recommend you'll give a try at libsorting.dll

The plugin contains procedure TSASort(), which supports several algorithms. :)
You can sort both ways with it aswell! (Low=>High [A, B, C, D] OR High=>Low [D, C, B, A])

..the plugin is open-source, so you can grab those TSA sorting algorithms from the plugin incase you don't want to use TSASort().

Included a small example to that topic, about sorting TStringArrays with it:

{$LOADLIB libsorting.dll}

var
TSA: array of string;

begin
TSA := ['Apple', 'Orange', 'Lemon', 'Banana', 'Pear'];
TSASort(TSA, sa_ShellSort, so_HighToLow); // Reversed.
WriteLn(ToStr(TSA));
TSASort(TSA, sa_MergeSort, so_LowToHigh); // Default.
WriteLn(ToStr(TSA));
end.

Which debugs:


Compiled successfully in 31 ms.
['Pear', 'Orange', 'Lemon', 'Banana', 'Apple']
['Apple', 'Banana', 'Lemon', 'Orange', 'Pear']
Successfully executed.

-Jani

gerauchert

2013-05-22 11:03:32

@Flight and @Jani:

Thanks so much for the help! This makes life so much easier for me :) Super fast alphabetizing; great dll. Thanks again

Dgby714

2013-05-22 13:57:22

For Lape =)

program new;
{$C+}{$B+}{$M+}{$X+}{$F-}{$S-}

type
TFilterMethod = procedure(const Self; var Index: UInt32);

procedure TIntegerArray.Filter(Method: TFilterMethod);
var
I: UInt32;
begin
for I := 0 to High(Self) do
Method(Self, I);
end;

procedure TStringArray.Filter(Method: TFilterMethod);
var
I: UInt32;
begin
for I := 0 to High(Self) do
Method(Self, I);
end;

procedure IntSort(const Self: TIntegerArray; var Index: UInt32);
begin
if (Index > 0) then
if (Self[Index] < Self[Index - 1]) then
begin
Swap(Self[Index], Self[Index - 1]);
Dec(Index, 2);
end;

if (Index < High(Self)) then
if (Self[Index] > Self[Index + 1]) then
begin
Swap(Self[Index], Self[Index + 1]);
Dec(Index);
end;
end;

procedure StrSort(const Self: TStringArray; var Index: UInt32);
begin
if (Index > 0) then
if (CompareText(Self[Index], Self[Index - 1]) < 0) then
begin
Swap(Self[Index], Self[Index - 1]);
Dec(Index, 2);
end;

if (Index < High(Self)) then
if (CompareText(Self[Index], Self[Index + 1]) > 0) then
begin
Swap(Self[Index], Self[Index + 1]);
Dec(Index);
end;
end;

var
Arr: TIntegerArray = [5, 3, 2, 4, 1];
Arr2: TStringArray = ['hello', 'world', 'I', 'am', 'Dgby'];

begin
Arr.Filter(IntSort);
WriteLn(Arr);

Arr2.Filter(StrSort);
WriteLn(Arr2);
end.