Hi, I want to sort a TStringList but not by the strings in it, but by the Objects. Here's my code:

var
  i : integer;
  iCount : integer;
  idxFound : integer;
  someText : string;
  s : TStringList;
  oneWord : string;

  aux : integer;
  
begin
  someText := memo_txtfile.text;
  oneWord := '';

  s := TStringList.Create;
  for i := 1 to length(someText) do begin
    aux:=ord(someText[i]);

    if aux<33 then begin

      idxFound := s.indexof(oneWord);
      if idxFound >= 0 then begin
        iCount := integer(s.objects[idxFound]);
        s.Objects[idxFound] := TObject(iCount + 1);
      end
      else begin
        s.AddObject(oneWord, TObject(1));
      end;
      oneWord := '';
    end
    else begin
      oneWord := oneWord + someText[i];
    end;
  end;

  if oneWord <> '' then
  begin
    idxFound := s.indexof(oneWord);
  end;
    if idxFound >= 0 then begin
      iCount := integer(s.objects[idxFound]);
      s.Objects[idxFound] := TObject(iCount + 1);
    end
    else begin
      s.AddObject(oneWord, TObject(1));
    end;

  // put the results on the screen in a text box.
  quicksort(s,0,s.Count-1);
  memo1.Text := '';
    memo1.Lines.Add('Frequency' + #9#9 + 'Word');
  for i := 0 to s.Count - 1 do
    memo1.Lines.Add(intToStr(integer(s.Objects[i])) + #9#9 + s[i]);
end;

This counts how many words are in the memo_txtfile.

Have a look at this example. It uses the TStringList CustomSort. You can use the same method to sort the by the objects.

I'm sorry but I quite can't understand how to use the object type. I find incompatible types when I try to adapt that code you show me.
Thank you and sorry for my bad english and poor programming skills, please help me.

I'm sorry but I quite can't understand how to use the object type. I find incompatible types when I try to adapt that code you show me.
Thank you and sorry for my bad english and poor programming skills, please help me.

SORRY I saw how to solve that. But as always, I keep getting errors.

Now when I'm trying to use, as your page says:

s.CustomSort(CompareInt)

I get two different errors:

1.- If I put the function inside the Button1Click event -> "Local procedure/function 'CompareInt' assigned to procedure variable.

Searching on google I found I needed to put the function out of the button1click event (at least I understood it like that). But I get...

2.- "Undeclared identifier 'ComparedInt' at line 20."

At least I'm step forward the fiiirst problem :D

Edited 5 Years Ago by Pundia: n/a

2. Typo ? A 'd' too much ...

Typo in the reply of this thread, not in actual delphi source code.

I had to figure it out myself. That's good, and thank you pritaeas for your help!
I used quick sort in descending order based on TObjects of the TStringList.

procedure QuickSort(var A: tstringlist; iLo, iHi: Integer) ;
var
   Lo, Hi, Pivot: Integer;
   aux:string;
   aux2:integer;
   T: TstringList;

begin
   Lo := iLo;
   Hi := iHi;
        aux:=  inttostr(integer(A.objects[Lo]));
     aux2:=integer(A.objects[Lo]);
   t := tstringlist.Create;
   T.add('a');
    Pivot := integer(A.objects[(Lo + Hi) div 2]);
   repeat

     while integer(A.objects[Lo]) > Pivot do Inc(Lo) ;
     while integer(A.objects[Hi]) < Pivot do Dec(Hi) ;
     if Lo <= Hi then
     begin
       T[0] := A[Lo];
       t.Objects[0]:= A.Objects[Lo];
       A[Lo] := A[Hi];
       A.Objects[Lo] := A.Objects[Hi];
       A[Hi] := T[0];
       A.Objects[Hi] := T.Objects[0];
       Inc(Lo) ;
       Dec(Hi) ;
     end;
   until Lo > Hi;
   if Hi > iLo then QuickSort(A, iLo, Hi) ;
   if Lo < iHi then QuickSort(A, Lo, iHi) ;
end;

This program is supposed to count words, but when somewhere in the text apears ' ' two or more spaces, the program thinks the last space is a word. Any ideas on how to solve this? I think I'll make another thread.

This question has already been answered. Start a new discussion instead.