Here is a helper class solution that sorts by DisplayName: you can improve sorting if you want, I used a TStringList to sort for me. The class helper is available wherever you refer to a device containing a helper class, so if you have a utility, put it there.
interface TCollectionHelper = class helper for TCollection public procedure SortByDisplayName; end; Implementation procedure TCollectionHelper.SortByDisplayName; var i, Limit : integer; SL: TStringList; begin SL:= TStringList.Create; try for i := self.Count-1 downto 0 do SL.AddObject(Items[i].DisplayName, Pointer(Items[i].ID)); SL.Sort; Limit := SL.Count-1; for i := 0 to Limit do self.FindItemID(Integer(SL.Objects[i])).Index := i; finally SL.Free; end; end;
Then, to use the method, simply pretend that it is a method of the TCollection class. This works in any subclass of TCollection.
MyCollection.SortByDisplayName
or MyCollectionItem.Collection.SortByDisplayName
.
source share