Solved

TList sort 2 items

Posted on 2004-08-25
4
239 Views
Last Modified: 2010-04-05
I'm making a class that looks like this...

type
  TUserInfo = class
  private
    Ffirst_name : string;
    Flast_name  : string;
    Fdid_ext    : string;
    Fdirect_no  : string;
    Fmobile_no  : string;
    Ftitle      : string;
    Fmail_addr  : string;
    Fdepartment : string;
  public
      property first_name : string
          read Ffirst_name;
      property last_name : string
          read Flast_name;
      property did_ext : string
          read Fdid_ext;
      property direct_no : string
          read Fdirect_no;
      property mobile_no : string
          read Fmobile_no;
      property title : string
          read Ftitle;
      property mail_addr : string
          read Fmail_addr;
      property department : string
          read Fdepartment;

      constructor Create(const Ffirst_name : string; const Flast_name  : string;
                         const Fdid_ext    : string; const Fdirect_no  : string;
                         const Fmobile_no  : string; const Ftitle      : string;
                         const Fmail_addr  : string; const Fdepartment : string);
  end;


I want to be able to sort this stuff on the fly in two scenarios..

Scenario 1: Sort by Last name
Scenario 2: Sort by Department (alphabetically) then by Last name

How would I go about doing this?

Also, what's the best way to clear the contents of a tlist in one shot?

0
Comment
Question by:gwarguitar
  • 3
4 Comments
 
LVL 2

Expert Comment

by:gary_williams
ID: 11897648
Write a sort-compare function that you can pass as an argument into TList.CustomSort.

The easiest way to clear a TList is to call the "Clear" method of TList.
0
 
LVL 2

Expert Comment

by:gary_williams
ID: 11897680
function SortCompareByLastName(Item1, Item2: Pointer): Integer;
var
  UserInfo1: TUserInfo;
  UserInfo2: TUserInfo;
begin
  UserInfo1 := Item1; // no typecast needed
  UserInfo2 := Item2;
  Result := CompareText(UserInfo1.Last_Name, UserInfo2.Last_Name);
end;

Then call List.Sort(SortCompareByLastName);

To sort by department and then by last name, call Sort twice, first passing SortCompareByLastName, then SortCompareByDepartment (which would be very similar).
0
 

Author Comment

by:gwarguitar
ID: 11898090
the first part works, but calling sort twice only resorts it back to whatever the last one called is.

any ideas there?

also, clear doesn't seem to work, anytime i rebuild a listview with tlist info, all my tlist info is displayed again...
i am calling the clear on the listview as well...
0
 
LVL 2

Accepted Solution

by:
gary_williams earned 250 total points
ID: 11899064
TList.Clear removes all items from the list.  You can watch List.Count before and after the call and you'll see for yourself.  The problem must lie elsewhere, but without seeing your code, I can't help much.

Sorting by the minor field and then by the major field should have the intended effect.  However, TList.Sort uses the QuickSort algorithm, and we really need at least the last of the two sorts to be stable (e.g. use ShellSort, etc.).

An alternative is to combine the two sorts into one SortCompare function.

 function SortCompareByDepartmentAndLastName(Item1, Item2: Pointer): Integer;
var
  UserInfo1: TUserInfo;
  UserInfo2: TUserInfo;
begin
  UserInfo1 := Item1; // no typecast needed
  UserInfo2 := Item2;
  Result := CompareText(UserInfo1.Department, UserInfo2.Department);
  if (Result = 0) then
    Result := CompareText(UserInfo1.Last_Name, UserInfo2.Last_Name);
end;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 69
QRReport  TQrmemo vertical stretching 1 42
Base1 Encode/Decode 3 68
RESTRequest Parameter 4 14
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now