Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to sort a TList Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
VCL-General
Language
Delphi 2.x
Views
68
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

How to sort a TList

Answer:

1   procedure BubbleSort(const List: TList; const Compare: TListSortCompare);
2   var
3     Limit: Integer;
4     I: Integer;
5     Temp: Pointer;
6     Swapped: Boolean;
7   begin
8     for Limit := (List.Count - 1) downto 1 do
9     begin
10      Swapped := False;
11      for I := 0 to (Limit - 1) do
12        if (Compare(List[I], List[I + 1]) > 0) then
13        begin
14          Temp := List[I];
15          List[I] := List[I + 1];
16          List[I + 1] := Temp;
17          Swapped := True;
18        end;
19      if (not Swapped) then
20        Break;
21    end;
22  end;
23  
24  procedure InsertionSort(const List: TList; const Compare: TListSortCompare);
25  var
26    Step: Integer;
27    Temp: Pointer;
28    I: Integer;
29  begin
30    for Step := 1 to (List.Count - 1) do
31    begin
32      Temp := List[Step];
33      for I := (Step - 1) downto 0 do
34        if (Compare(List[I], Temp) > 0) then
35          List[I + 1] := List[I]
36        else
37          Break;
38      List[I + 1] := Temp;
39    end;
40  end;
41  
42  procedure ShellSort(const List: TList; const Compare: TListSortCompare);
43  var
44    Step: Integer;
45    H: Integer;
46    I: Integer;
47    Temp: Pointer;
48  begin
49    H := 1;
50    while (H <= (List.Count div 9)) do
51      H := 3 * H + 1;
52    while (H > 0) do
53    begin
54      for Step := H to (List.Count - 1) do
55      begin
56        Temp := List[Step];
57        I := Step - H;
58        while (I >= 0) do
59  
60        begin
61          if (Compare(Temp, List[I]) < 0) then
62            List[I + H] := List[I]
63          else
64            Break;
65          Dec(I, H);
66        end;
67        List[I + H] := Temp;
68      end;
69      H := H div 3;
70    end;
71  end;
72  
73  procedure QuickSort1(const List: TList; const Compare: TListSortCompare;
74    const L: Integer; const R: Integer);
75  var
76    I: Integer;
77    J: Integer;
78    Temp: Pointer;
79  begin
80    I := L - 1;
81    J := R;
82    repeat
83      Inc(I);
84      while (Compare(List[I], List[R]) < 0) do
85        Inc(I);
86      Dec(J);
87      while (J > 0) do
88      begin
89        Dec(J);
90        if (Compare(List[J], List[R]) <= 0) then
91          Break;
92      end;
93      if (I >= J) then
94        Break;
95      Temp := List[I];
96      List[I] := List[J];
97      List[J] := Temp;
98    until
99      (False);
100   Temp := List[I];
101   List[I] := List[R];
102   List[R] := Temp;
103 end;
104 
105 procedure QuickSort(const List: TList; const Compare: TListSortCompare);
106 begin
107   QuickSort1(List, Compare, 0, List.Count - 1);
108 end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC