?
Solved

sort a list randomly

Posted on 2007-10-06
3
Medium Priority
?
267 Views
Last Modified: 2010-08-05
hi all

does somebody have some code to sort a list randomly?

thanks
0
Comment
Question by:zattz
  • 2
3 Comments
 
LVL 5

Expert Comment

by:xr1140
ID: 20027822
take a look




uMain.dfm
=========================


object Form1: TForm1
  Left = 416
  Top = 139
  Width = 438
  Height = 428
  Caption = 'randomSort'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 200
    Top = 20
    Width = 221
    Height = 361
    TabOrder = 0
  end
  object Button1: TButton
    Left = 20
    Top = 30
    Width = 156
    Height = 41
    Caption = '1. build a ordered list'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 25
    Top = 305
    Width = 141
    Height = 41
    Caption = '2. sort random that list'
    TabOrder = 2
    OnClick = Button2Click
  end
end








uMain.pas
=========================



unit uMain;

interface

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

type
  PMyList = ^AList;
  AList = record
    id : integer;
    name : string;
  end;

  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  lst : TList;


implementation

uses Math;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  item: PMyList;
  i, j : integer;
begin
  lst := TList.Create;

  for i := 0 to 25 do begin
    New(item);
    item^.id := i;
    item^.name := 'Item #' + IntToStr(i);
    lst.Add(item);
  end;

  for j := 0 to (lst.Count-1) do begin
    item := lst.Items[j];

    memo1.Lines.Add( item.name );
  end;

end;

procedure TForm1.Button2Click(Sender: TObject);
var
  tmpLst : TList;
  item: PMyList;
  i, j : integer;
begin
  Randomize;
  tmpLst := TList.Create;

  for i := 0 to (lst.Count-1) do begin
    item := lst.Items[i];

    lst.Move(i, random(lst.Count-1) );
  end;

  memo1.Clear;
  for j := 0 to (lst.Count-1) do begin
    item := lst.Items[j];

    memo1.Lines.Add( item.name );
  end;

end;

end.
0
 
LVL 5

Accepted Solution

by:
xr1140 earned 2000 total points
ID: 20027835


oups, seems i`ve forgot to clean the code. It works ok, but you can replace the code for Button2Click with this:


procedure TForm1.Button2Click(Sender: TObject);
var
  item: PMyList;
  i, j : integer;
begin
  Randomize;

  for i := 0 to (lst.Count-1) do
    lst.Move(i, random(lst.Count-1) );

  memo1.Clear;
  for j := 0 to (lst.Count-1) do begin
    item := lst.Items[j];

    memo1.Lines.Add( item.name );
  end;

end;
0
 

Author Comment

by:zattz
ID: 20028103
thanks. works great.

but im posting another question thats .NET specific
0

Featured Post

Industry Leaders: 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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

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