title: Custom Sort a Delphi TStringList name/value pairs as integers
body: <p>I have a TStringList of Name/Value pairs. The names are all integer values 9stored as strings of course) and the values are all strings (comma separated).</p>
  
  <p>e.g.</p>
  
  <pre><code>5016=Catch the Fish!,honeyman,0
  30686=Ozarktree1 Goes to town,ozarktreel,0
  </code></pre>
  
  <p>.
  .
  .</p>
  
  <p>I would like to call the add routine and add new lines in the TStringlist, but need a way to sort the list afterwards.</p>
  
  <p>e.g.</p>
  
  <pre><code>Tags.Add(frmTag.edtTagNo.Text + '=' +
           frmTag.edtTitle.Text + ',' +
           frmTag.edtCreator.Text + ',' +
           IntToStr(ord(frmTag.cbxOwned.Checked)));
  Tags.Sort;
  </code></pre>
  
  <p>Here is what I tried:</p>
  
  <pre><code>Tags:= TStringList.Create;
   Tags.CustomSort(StringListSortComparefn);
   Tags.Sorted:= True;
  </code></pre>
  
  <p>my custom sort routine:</p>
  
  <pre><code>function StringListSortComparefn(List: TStringList; Index1, Index2: Integer): Integer;
  var
   i1, i2 : Integer;
  begin
   i1 := StrToIntDef(List.Names[Index1], 0);
   i2 := StrToIntDef(List.Names[Index2], 0);
   Result:= CompareValue(i1, i2);
  end;
  </code></pre>
  
  <p>However, it still seems to be sorting them like strings instead of integers.</p>
  
  <p>I even tried creating my own class:</p>
  
  <pre><code>type
   TXStringList = class(TStringList)
   procedure Sort;override;
  end;
  
  implementation
  
  function StringListSortComparefn(List: TStringList; Index1, Index2: Integer): Integer;
  var
  i1, i2 : Integer;
  begin
  i1 := StrToIntDef(List.Names[Index1], 0);
  i2 := StrToIntDef(List.Names[Index2], 0);
  Result:= CompareValue(i1, i2);
  end;
  
  procedure TXStringList.Sort;
  begin
   CustomSort(StringListSortComparefn);
  end;
  </code></pre>
  
  <p>I even tried some examples on SO (e.g. <a href="https://stackoverflow.com/questions/15423832/sorting-tstringlist-names-property-as-integers-instead-of-strings">Sorting TSTringList Names property as integers instead of strings</a>)</p>
  
  <p>Can someone tell me what I am doing wrong? Everytime, the list gets sorted as strings and not as integers.</p>
  
  <pre><code>30686=Ozarktree1 Goes to town,ozarktreel,0
  5016=Catch the Fish!,honeyman,0
  </code></pre>