Solved

Sort memo by length

Posted on 2006-11-24
5
552 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:
2266180 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

679 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