Solved

Sort memo by length

Posted on 2006-11-24
5
545 Views
Last Modified: 2010-04-04
Hi,

Does anybody have some code that will sort the lines in a memo by length? longest first.

Thanks
0
Comment
Question by:zattz
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 18008671
Length of what? Line, sentence, word or something else?
0
 

Author Comment

by:zattz
ID: 18008698
line
0
 
LVL 28

Accepted Solution

by:
ciuly earned 500 total points
ID: 18008956
try this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function mysort(List: TStringList; Index1, Index2: Integer): Integer;
begin
  result:=length(list[index2])-length(list[index1]);
end;

procedure TForm1.FormCreate(Sender: TObject);
var s:TStringList;
begin
  memo1.lines.add('213');
  memo1.lines.add('rewrqwte');
  memo1.lines.add('w');
  memo1.lines.add('ererer');
  s:=TStringList.Create;
  s.AddStrings(memo1.lines);
  s.CustomSort(mysort);
  memo1.lines.clear;
  memo1.lines.AddStrings(s);
  s.free;
end;

end.


0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 18010390
I would add

  memo1.lines.BeginUpdate;
  memo1.lines.clear;
  memo1.lines.AddStrings(s);
  memo1.lines.EndUpdate;

to speed it up and get less screen activity.
0
 

Author Comment

by:zattz
ID: 18010489
Good idea:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now