Sultan Mustafa
Honorary Master
- Joined
- Nov 7, 2020
- Messages
- 24,866
- Reaction score
- 17,097
Ok, so for once I'm posting about something else than an assignment, lol. But now I face another problem, long ago I wrote a point keeping program for the local stock car club. Been working fine until June this year.
Now my sort functions have gone rogue, and I'm stuck on how to solve this. I made this program with Delphi btw.
Background:
So in a race, there are a bunch of classes (Hotrods, 2.1s, etc). And every class has a certain amount of heats to race. Now you enter the first two heats results, whether they came first or whatever since every position finish has its own points value (I checked and the point conversion is completely fine), then it automatically adds the results of the two heats into the Subtotal 1 column (which is used to determine starting positions for Heat 3). And it works similarly for Heat 3 and onwards. The columns that can be sorted are Sub 1, Sub 2 and the Final Heat as well as the final Total.
Sub 1 has to be sorted from smallest to highest (which will sort the grid). All the other columns have to be sorted from largest to smallest. The problem is there is no problem with the sorting themselves but I get wonky results... It's as if it just randomizes the sorting... And it worked perfectly until now...
Example:


Code:
Now my sort functions have gone rogue, and I'm stuck on how to solve this. I made this program with Delphi btw.
Background:
So in a race, there are a bunch of classes (Hotrods, 2.1s, etc). And every class has a certain amount of heats to race. Now you enter the first two heats results, whether they came first or whatever since every position finish has its own points value (I checked and the point conversion is completely fine), then it automatically adds the results of the two heats into the Subtotal 1 column (which is used to determine starting positions for Heat 3). And it works similarly for Heat 3 and onwards. The columns that can be sorted are Sub 1, Sub 2 and the Final Heat as well as the final Total.
Sub 1 has to be sorted from smallest to highest (which will sort the grid). All the other columns have to be sorted from largest to smallest. The problem is there is no problem with the sorting themselves but I get wonky results... It's as if it just randomizes the sorting... And it worked perfectly until now...
Example:


Code:
Code:
procedure TfrmPuntehou.SortLTSGrid(var grid: TStringGrid; columntotal: Integer);
var
TheList : TStringList;
i,l,iCount,m:integer;
const
separator = ',';
begin
//sorts grid largest to smallest according to one column
//get grid row amount
iCount:=grid.RowCount - 1;
//create and fill the string list
TheList := TStringList.Create;
//fill the list
for i := 1 to (iCount) do
begin
TheList.Add(grid.Rows[i].Strings[columntotal]+separator+grid.Rows[i].Text);
end;
//try block to sort and write all strings in the list to the grid correctly
try
TheList.CustomSort(Compare2);
for i:= 1 to (iCount) do
begin
grid.Rows[i].Text := TheList.Strings[(i-1)] ;
end;
//fill the row numbers
for m := 1 to iCount do
begin
grid.Cells[0,m]:= IntToStr(m);
end;
finally
TheList.Free;
end;
end;
Code:
procedure TfrmPuntehou.SortSTLGrid(var grid:TStringGrid; columntotal:integer);
const
separator = ',';
var
iCount,i,j,k,iPos:integer;
TheList:TStringList;
sString,sTempString:string;
m: Integer;
l: Integer;
begin
//procedure to sort from small to large values
//get row amount
iCount:=grid.RowCount - 1;
//create list
TheList:=TStringList.Create;
TheList.Sorted:=False;
//start of try..finally block
try
begin
//fill the list
for i := 1 to (iCount) do
begin
for l := 1 to Length(arrCodes) do
begin
if grid.Rows[i].Strings[columntotal] = arrCodes[l] then
begin
TheList.Add('0'+separator+grid.Rows[i].Text);
end;
end;
TheList.Add(grid.Rows[i].Strings[columntotal]+separator+grid.Rows[i].Text);
end;
//sort the list
TheList.Sort;
for k := 1 to TheList.Count do
begin
//take the line of the list and put it in a string var
sString:= TheList.Strings[(k-1)];
//get separator pos in that string
iPos:=AnsiPos(separator,sString);
sTempString:='';
//remove separator and the column text at the front of the string
sTempString:=Copy(sString,(iPos+2),Length(sString));
TheList.Strings[(k-1)]:= '';
TheList.Strings[(k-1)]:= sTempString;
end;
//fill the grid
for j:= 1 to (iCount) do
begin
grid.Rows[j].Text := TheList.Strings[(J-1)] ;
end;
//fill the row numbers
for m := 1 to iCount do
begin
grid.Cells[0,m]:= IntToStr(m);
end;
end;
finally
TheList.Free;
end;
//end of try..finally block
end;