Re: Strings in file
- From: "Jon Jacobs" <JonJacobsAtComcast.net>
- Date: Fri, 23 Sep 2005 10:22:46 -0500
> if you have any idea cant you help me insted?
You will need to use a variation on Chris Willig's idea of a custom sort in
a TStringList. You would use the Copy function to build the comparison
strings to use in the _Sort function. (It does not matter if you name it
_Sort or something else.) The main thing is that you would perform some test
on the List[Index1] and List[Index2] and return 0, 1, or -1. 0 if the
comparisons are equal, 1 (or any positive integer) if the string at Index1
should come after the string at Index2, -1 (or any negative integer) if the
string at Index1 should come before the string at Index2.
See Delphi Help on TStringList | CustomSort
I will use the term "sort field" which means a portion of the complete
string. That portion, or portions is the basis for comparing one complete
string with another. You would concatenate the sort fields to make the
comparison string for each complete string.
If the sort fields of the strings in the file are always at the same
locations and with the same length, then building the comparison string will
be very easy. Even if the sort fields are not at the exact same location in
every string, but they are always the same length, the comparison would
still be super simple, along the lines of:
if comparestring1 = comparestring2 then
Result := 0
else if comparestring1 > comparestring2 then
Result := 1
else
Result := -1;
In my example comparestring1 is built from the sort fields of List[Index1]
and comparestring2 is built from the sort fields of List[Index2].
If the lengths of the sort fields are not always the same, you may need to
build a number from them (using StrToIntDef, for example) and compare the
numbers.
If the positions of the sort fields are not constant then you may have to
use Pos(SomeIdentifyingPattern) to find the fields in each string. If you
can not count on a consistent pattern of characters or location to find the
sort fields in each string, then you will have to come up with a more
complex alogorithm to locate your sort fields and build your compare string.
Only you, with your familiarity with the nature of the data, would be able
to come up with the proper parsing algorithm.
In any case, once you can identify the sort fields in each string, you would
use the facilities of the CustomSort method of the TStringList.
Jon Jacobs
.
- Follow-Ups:
- Re: Strings in file
- From: Tom
- Re: Strings in file
- References:
- Strings in file
- From: Tom
- Re: Strings in file
- From: Maarten Wiltink
- Re: Strings in file
- From: Tom
- Strings in file
- Prev by Date: Re: Strings in file
- Next by Date: Re: reading a opened file
- Previous by thread: Re: Strings in file
- Next by thread: Re: Strings in file
- Index(es):
Relevant Pages
|