gwarguitar
asked on
TList sort 2 items
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?
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?
function SortCompareByLastName(Item 1, 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(SortCompareByLas tName);
To sort by department and then by last name, call Sort twice, first passing SortCompareByLastName, then SortCompareByDepartment (which would be very similar).
var
UserInfo1: TUserInfo;
UserInfo2: TUserInfo;
begin
UserInfo1 := Item1; // no typecast needed
UserInfo2 := Item2;
Result := CompareText(UserInfo1.Last
end;
Then call List.Sort(SortCompareByLas
To sort by department and then by last name, call Sort twice, first passing SortCompareByLastName, then SortCompareByDepartment (which would be very similar).
ASKER
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...
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...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The easiest way to clear a TList is to call the "Clear" method of TList.