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
Solved

Sort memo by length

Posted on 2006-11-24
5
550 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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