Coh3n
2010-02-09 22:36:23
I need to sort a string array in alphabetical order for a school assignment program. Right now, it works for the first two letters in the string. I need help making it check the next letter if necessary, and keep doing it until everything is sorted properly.
Coh3n
program Alphabetical_Sorting;
function alpha_GetLetters(s: TStringArray; index: Integer): Array of String;
var
i: Integer;
begin
SetLength(Result, Length(s));
for i := 0 to High(s) do
Result[i] := Lowercase(StrGet(s[i], index));
end;
function alpha_SortLetters(s: TStringArray; index: Integer): Array of String;
var
alphabet: TStringArray;
letters: Array of String;
letterVals: Array of Integer;
hi, i, j: Integer;
begin
SetLength(Result, Length(s));
SetLength(letterVals, Length(s));
letters := alpha_GetLetters(s, index);
alphabet := ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
hi := High(alphabet);
for i := 0 to High(s) do
for j := 0 to hi do
if (letters[i] = alphabet[j]) then
letterVals[i] := j;
BubbleSort(letterVals);
for i := 0 to High(letterVals) do
for j := 0 to hi do
if (letterVals[i] = j) then
Result[i] := alphabet[j];
end;
function alpha_Occurrences(s: TStringArray; index: Integer): Array of Integer;
var
sorted: Array of String;
occurrences, hi, i, j: Integer;
begin
SetLength(Result, 0);
sorted := alpha_SortLetters(s, index);
hi := High(sorted);
for i := 0 to hi do
begin
if (i > 0) then
if (sorted[i] = sorted[i - 1]) then
Continue;
for j := 0 to hi do
if (sorted[i] = sorted[j]) then
Inc(occurrences);
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := occurrences;
occurrences := 0;
end;
end;
function alpha_SortWords(s: TStringArray; index: Integer): Array of String;
var
letters, sorted: Array of String;
i, j, k: Integer;
begin
SetLength(Result, 0);
letters := alpha_GetLetters(s, index);
sorted := alpha_SortLetters(s, index);
for j := 0 to High(sorted) do
for k := 0 to High(letters) do
begin
if (InStrArr(s[k], Result, False)) then
Continue;
if (sorted[j] = letters[k]) then
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := s[k];
end;
end;
end;
function alpha_GetDuplicates(s: TStringArray; index: Integer): Array of Array of String;
var
sorted: Array of String;
occurrences: Array of Integer;
temp, start, finish, i, j, k: Integer;
begin
sorted := alpha_SortWords(s, index);
occurrences := alpha_Occurrences(sorted, index);
for i := 0 to High(occurrences) do
begin
SetLength(Result, i + 1);
for j := 0 to i do
temp := temp + occurrences[j];
start := (temp - occurrences[i]);
finish := (temp - occurrences[i]) + (occurrences[i] - 1);
for k := start to finish do
begin
SetLength(Result[i], Length(Result[i]) + 1);
Result[i][High(Result[i])] := sorted[k];
end;
temp := 0;
end;
end;
function alpha_SortDuplicates(s: TStringArray; index: Integer): Array of Array of String;
var
duplicates: Array of Array of String;
i: Integer;
begin
duplicates := alpha_GetDuplicates(s, index);
SetLength(Result, Length(duplicates));
for i := 0 to High(duplicates) do
Result[i] := alpha_SortWords(duplicates[i], index + 1);
end;
function alpha_SortStringArray(s: TStringArray): Array of String;
var
index, i, j: Integer;
sorted, dupSorted: Array of String;
duplicates: Array of Array of String;
begin
Inc(index);
sorted := alpha_SortWords(s, index);
duplicates := alpha_GetDuplicates(sorted, index);
for i := 0 to High(duplicates) do
begin
if (Length(duplicates[i]) > 1) then
duplicates[i] := alpha_SortWords(duplicates[i], index + 1);
for j := 0 to High(duplicates[i]) do
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := duplicates[i][j];
end;
end;
end;
var
theArray, strings: Array of String;
i: Integer;
begin
ClearDebug;
strings := ['Chair', 'Bed', 'Cabnet', 'Tape', 'Book', 'Lamp', 'Light',
'Table', 'Shelf', 'Door', 'Binder'];
theArray := alpha_SortStringArray(strings);
for i := 0 to High(theArray) do
Writeln(theArray[i]);
end.
Thanks in advance,Coh3n