Board index » delphi » Sorting A String Grid with a secondary sort field

Sorting A String Grid with a secondary sort field

You can do double-sort routine:

first sort by date, and then by name. The names will be in alphabetical
order (a-z or z-a -> depending on way you compare) and dates will be sorted
too :)

Egon

 

Re:Sorting A String Grid with a secondary sort field


Quote
> What I want to do is combine these.  I want to sort it by say Delivery
Date,
> with a sub sort on the Customer Name field.  This way the dates are
all in
> order (oldest date at top, newest at bottom) and the customers are all
in
> alphabetical order well at least on any specific date.

It's easy. :-) Do the sort the same way you do now for the date column
(eg., Delivery Date), but do your comparison on both the date and the
customer name. IOW, instead of just comparing two dates, compare date1 +
name 1 with date2 + name 2. (One suggestion: When you sort by date, make
sure you're using an ANSI format like 'CCYYMMDD', as in '20030122'; this
makes the sort correct according to year.)

Ken
---
Ken White
kwh...@adpsi.com

Clipper Functions for Delphi and C++ Builder
http://www.adpsi.com

Re:Sorting A String Grid with a secondary sort field


Quote
Ken White wrote:
> It's easy. :-) Do the sort the same way you do now for the date column
> (eg., Delivery Date), but do your comparison on both the date and the
> customer name. IOW, instead of just comparing two dates, compare date1 +
> name 1 with date2 + name 2. (One suggestion: When you sort by date, make
> sure you're using an ANSI format like 'CCYYMMDD', as in '20030122'; this
> makes the sort correct according to year.)

> Ken
> ---
> Ken White
> kwh...@adpsi.com

> Clipper Functions for Delphi and C++ Builder
> http://www.adpsi.com

I actually have the date in a more human readable format 1/13/03 and I
am using StrToDate to convert them to TDate format so I can Compare them
  as if Date1 > Date2 then swap them.

How do I compare the dates and the customer names like this?  Should I
convert the date into the ANSI format above if so how do I do that?

Thanks,
Derek

Re:Sorting A String Grid with a secondary sort field


David,

You might try looking at the articles on ClientDataset by Cary Jensen on the
Borland communities website.  Apart from being clear and informative, one of
them contains a generic sorting capabilit implemented using ClientDatasets.
This does not answer your specific question, but the article might be of
interest if you are playing around with grid sorting.

Pete K

Re:Sorting A String Grid with a secondary sort field


To put the dates into a consistent Ansi format for each one try:

Date1:=FormatDateTime('yyyymmdd', strToDate(DateVariable));

To compare one date against the other:

if CompareStr(Date1, Date2)>0 then

If you have more fields, simply combine them in the same order (converting
them to a string if need be) and execute a compareStr on the lot.  If you
want it to be case insensitive I think you use CompareText, but check the
help.

When adding a string field to the strings to be compared, it is a good idea
to do a trim first and, if adding another field after it, add a  space at
the end, the reason for this is that a combination of strings to be compared
might look like

Fax20011021
Fax Letter20011021

On compare, the first would register as greater than the second, because the
second has a space as its 4th character and the first has a 2.  If you
separate the string fields with a ' '  (ie Fax 20011021) then the second is
greater than the first, because L is greater than 2).

Incidentally, if you are comparing fields with numbers, make sure that the
numbers are of the same length.  I think  236 would register as greater than
1234 in a compare text function.  If you add a space (or 0) at the beginning
of 236 this would not happen.  So each time adding a number field to the
string to be compared, take the maximum number of digits in the number field
calculate the length of the number value and then insert sufficient spaces
or 0s to bring the length of the field up to the maximum.

I hope this is of some help.

BTW, if you have a reasonably large table to sort (say 1000 or more records)
this sort of non-indexed sort will take a fairly long (and, for end users,
an unacceptable time).

For a large table, try and implement some form of quasi indexing.

Regards,

Mark Williams

Re:Sorting A String Grid with a secondary sort field


Thanks for all your help.  This works just as I wanted.

Thanks again,

Derek

Quote
Mark Williams wrote:
> To put the dates into a consistent Ansi format for each one try:

> Date1:=FormatDateTime('yyyymmdd', strToDate(DateVariable));
> Regards,

> Mark Williams

Re:Sorting A String Grid with a secondary sort field


Derek,

Quote
> I actually have the date in a more human readable format 1/13/03 and I
> am using StrToDate to convert them to TDate format so I can Compare
them
>   as if Date1 > Date2 then swap them.

But you don't need to do this; you can directly compare the dates in
ANSI format:

if '20030101' > '20021231' then ...

Quote
> How do I compare the dates and the customer names like this?  Should I

So you just expand the above:

sCust1 := 'Smith, Jane';
sCust2 := 'Jones, Frank';
sDate1 := '20030101';

if (sDate1 + sCust1) > (sDate1 + sCust2) then...

Quote
> convert the date into the ANSI format above if so how do I do that?

FormatDateTime('yyyymmdd', TheDateValue) will give you the ANSI
formatted date.

Ken
---
Ken White
kwh...@adpsi.com

Clipper Functions for Delphi and C++ Builder
http://www.adpsi.com

Other Threads