Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sort memo by length

Posted on 2006-11-24
5
Medium Priority
?
571 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

664 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