Menu

Commit [r4340]  Maximize  Restore  History

Added new TSnippetTitleComparator class for use in comparing ISnippet titles.

delphidabbler 2013-12-16

changed /branches/parsnip/Src/Main/CS.Database.Snippets.pas
/branches/parsnip/Src/Main/CS.Database.Snippets.pas Diff Switch to side-by-side view
--- a/branches/parsnip/Src/Main/CS.Database.Snippets.pas
+++ b/branches/parsnip/Src/Main/CS.Database.Snippets.pas
@@ -17,15 +17,30 @@
 interface
 
 uses
+  // Delphi
   Generics.Collections,
+  // Project
   CS.ActiveText,
-  Compilers.UGlobals,
   CS.Database.Types,
   CS.SourceCode.Languages,
   CS.Utils.Dates,
+  Compilers.UGlobals,
   IntfCommon,
+  UComparers,
   UExceptions,
   UIStringList;
+
+type
+  ///  <summary>Comparer for snippets by title.</summary>
+  TSnippetTitleComparator = class(TComparator<ISnippet>)
+  public
+    ///  <summary>Compares snippets Left and Right. Returns -ve if Left's
+    ///  title sorts before Right's, 0 if the same or +ve if Left's title is
+    ///  greater than Right's.</summary>
+    function Compare(const Left, Right: ISnippet): Integer; override;
+    function Equals(const Left, Right: ISnippet): Boolean; override;
+    function GetHashCode(const Snippet: ISnippet): Integer; override;
+  end;
 
 type
   TSnippetBase = class(TInterfacedObject)
@@ -179,12 +194,16 @@
 implementation
 
 uses
+  // Delphi
   SysUtils,
   Generics.Defaults,
   Classes,
   RTLConsts,
+  // Project
   CS.Database.SnippetLinks,
-  CS.Database.Tags;
+  CS.Database.Tags,
+  CS.Utils.Hashes,
+  UStrUtils;
 
 { TSnippetIDList }
 
@@ -576,5 +595,22 @@
   UpdateFrom(AOther as TReadOnlySnippet);
 end;
 
+{ TSnippetTitleComparator }
+
+function TSnippetTitleComparator.Compare(const Left, Right: ISnippet): Integer;
+begin
+  Result := StrCompareText(Left.Title, Right.Title);
+end;
+
+function TSnippetTitleComparator.Equals(const Left, Right: ISnippet): Boolean;
+begin
+  Result := StrSameText(Left.Title, Right.Title);
+end;
+
+function TSnippetTitleComparator.GetHashCode(const Snippet: ISnippet): Integer;
+begin
+  Result := TextHash(Snippet.Title);
+end;
+
 end.
 
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.