Solved

Sort memo by length

Posted on 2006-11-24
5
555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…

710 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