Re: Strings in file



Tom wrote:
Sorry for my english!
If i have 5 lines in a .txt file: like this

"This is line :345"
"This is line :323
"This is line :356"
"This is line :385"
"This is line :312"

I want all information in the txtfile to be visible in a memo (no problem)
but when i push the button i want to sort them in numbers?, what if the
string have more numbers after eachouther like this:
"This is line :312 and has the number 412".
but i only want to sort them by the first set of numbers

I need a button for the lowest number and one for the highest number
Can someone help me with this



Something like this:

         function FirstInteger(const s: string): integer;
         var
           a, b : integer;
         begin
           result := 0;

           if s <> '' then begin
             a := 1;
             while (a <= Length(s)) and not(s[a] in ['0'..'9']) do
               Inc(a);

             if a <= Length(s) then begin
               b := a;
               while (b <= Length(s)) and (s[b] in ['0'..'9']) do
                 Inc(b);

               result := StrToIntDef(Copy(s, a, b -a), 0);
             end;
           end;
         end;

function _Sort(List: TStringList; Index1, Index2: Integer): Integer;
begin
result := FirstInteger(List[Index1]) -FirstInteger(List[Index2]);
end;


//load txtfile into memo (sorted)
var
  Strs : TStringList;
begin
  Strs := TStringList.Create;
  try
    Strs.LoadFromFile( YourTxtFile ); //load file into stringlist
    Strs.CustomSort(@_Sort);          //sort
    Memo1.Lines.Assign(Strs);         //put in memo
  finally
    Strs.Free;
  end;

  //Strs[0] is lowest
  //Strs[Strs.Count -1] is highest
end;
.


Quantcast