Natural sorting optimizing... and a working solution :)




I have been working with a natural sorting solution today and i was
wondering if someone could have a look and maybe suggest how to optimize it.
It works, but it is ugly code. It will for sure not work with tiburian
strings. How should I work with strings when I need to extract each char
Well anyway it is a good starting point if you are looking for this kind of
sorting. I have looked very hard without finding a single delphi
implementation so here is your chance
I am using the code to sort filenames.

Here is the code:

function CompareNatural(s1, s2: String): Integer;
var
n1, n2, j: Integer;
c1, c2: Char;
t1, t2: String;
b: Boolean;

function ExtractNr(start: Integer; var txt: String): Integer;
var
n: Integer;
s: String;
begin
Result:= 0;
n:= start;
while (n #47) and (txt[n] < #58) do
n:= n + 1;
s:= Copy(txt, start, (n - start));
Delete(txt, start - 1, (n - start) + 1);
if s '' then
Result:= StrToInt(s)
end;

begin
t1:= LowerCase(s1);
t2:= LowerCase(s2);
j:= 1;
b:= False;
while (not b) do
begin
result:= 0;
c1:= t1[j];
c2:= t2[j];
if c1 c2 then
begin // Check if number...
if ((c1 > #47) and (c1 < #58)) and ((c2 > #47) and (c2 < #58)) then
begin
n1:= ExtractNr(j, s1);
n2:= ExtractNr(j, s2);
if n1 = n2 then
Result := 0
else
begin
if n1 > n2 then
Result := 1
else
Result := -1;
b:= True;
end;
end else
begin
if c1 = c2 then // Check if chars are equal
Result := 0
else
begin
if c1 > c2 then
Result := 1
else
Result := -1;
b:= True;
end;
end;
b:= b or (j = Min(Length(t1), Length(t2)));
end;
b:= b or (j = Min(Length(t1), Length(t2)));
j:= j + 1;
end;
if result = 0 then
begin
if Length(t1) Length(t2) then
if Length(t1) > Length(t2) then
Result:= 1
else
Result:= -1;
end;
end;

Posted On: Saturday 22nd of September 2012 01:41:03 AM Total Views:  113
View Complete with Replies

RELATED TOPICS OF Delphi Programming PROGRAMMING LANGUAGE




TJvDBUltimGrid sorting

I've posted this to the components general group, but no response. I'm trying to learn how to sort a column using TJvDBUltimGrid, however, I'm getting the following error when I click on a column: Project xxxxx raised exception class EJVCLDbGridException with message 'Cannot sort. An index property is missing'. Can someone give me a push in the right direction I'll also need to add dropdowns in some of the columns, is there an example or advice Example source would be great. Rich
VIEWS ON THIS POST

84

Posted on:

Wednesday 19th September 2012
View Replies!

Rant: Don't want Unicode just give me a working IDE

Today I had to install RAD Studio on my Vista notebook. I'll be on a trip to make some system installation in another city and I'm going to use my notebook as a mobile developement station. Anyway, I installed RAD studio (only Delphi for Win32) which was flawless. I tried testing one of my project in newly installed Delphi and it started to give strange errors. It complained about missing DCU units. I checked the search path and it was correct. I have copied exactly the same directory structure from my Desktop XP PC to my Vista notebook. I checked all the PAS files and there were in the place. I re-checked the search path for both Debug/Release configurations and again Delphi refused to compile my project. If I go and add so-called missing PAS files to the project then it works but how could I add more than 100 files manually. Being a Delphi user for 13 years I was baffled with no clue at hand. I restarted my notebook and this time things started to be more strange. It compiled one of my projects but refused the other one. It was like a nightmare. One of the compiled project produced no output at all. It was a Library and though it was compiled successfully the resulted BPL was no where in my Harddrive. path settings were correct. required files were in place but Delphi refused to compile. Exactly the same setup on my XP desktop works fine. Has anyone else faced such a weird behavior from Delphi 2007 under Vista I didn't had time to go through the details of the problem. It wasted one D2007 activation key and costed me 2 hours with lots of frustrations. Time was running fast. I uninstalled D2007 and installed a pirated copy of Delphi 7.0. (Sorry but didn't feel guilty at all) Delphi 7.0 was working fine. It compiled everything as it should. I started wondering which one is Vista comptabile D7 or D2007. Fortunately my projects can be compiled in D7 too so I can go and do my installation.
VIEWS ON THIS POST

190

Posted on:

Wednesday 19th September 2012
View Replies!

"val" not working with currency variables?

all, I'm trying to read numbers from an INI file and convert them into currencies, but much to my surprise the compiler throws a "left side cannot be assigned to" exception in the line that contains the "val" keyword. Is "Val" non-functional with currency variables I cannot use "strtofloat" due to locale issues. Function twellpowersettings.get_interpolationinterval (columnname:ansistring) : currency; var s:Ansistring; er:integer; Begin s := ini.readstring('Interpolation Interval', ColumnName,'9999'); val(s,result,er); If er0 then result:=9999; End; -- Arthur Hoornweg (In order to reply per e-mail, please just remove the ".net" from my e-mail address. Leave the rest of the address intact including the "antispam" part. I had to take this measure to counteract unsollicited mail.)
VIEWS ON THIS POST

221

Posted on:

Friday 21st September 2012
View Replies!