Недавно добавленные исходники

•  DeLiKaTeS Tetris (Тетрис)  136

•  TDictionary Custom Sort  3 317

•  Fast Watermark Sources  3 065

•  3D Designer  4 825

•  Sik Screen Capture  3 320

•  Patch Maker  3 535

•  Айболит (remote control)  3 637

•  ListBox Drag & Drop  2 996

•  Доска для игры Реверси  81 564

•  Графические эффекты  3 927

•  Рисование по маске  3 231

•  Перетаскивание изображений  2 613

•  Canvas Drawing  2 735

•  Рисование Луны  2 561

•  Поворот изображения  2 166

•  Рисование стержней  2 161

•  Paint on Shape  1 564

•  Генератор кроссвордов  2 226

•  Головоломка Paletto  1 764

•  Теорема Монжа об окружностях  2 215

•  Пазл Numbrix  1 682

•  Заборы и коммивояжеры  2 052

•  Игра HIP  1 279

•  Игра Go (Го)  1 225

•  Симулятор лифта  1 471

•  Программа укладки плитки  1 214

•  Генератор лабиринта  1 542

•  Проверка числового ввода  1 352

•  HEX View  1 490

•  Физический маятник  1 355

 
скрыть


Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Сортировать TListView, используя нужный метод



Оформил: DeeCo

{ 
  This example shows how to use the TListView's CustomSort method to 
  sort the items in the list using a ordering function. 
  This allows you to sort custom data in the correct order you want. 

  When the user clicks on a column header, the ListView will be sorted 
  by that column. 
  If the user clicks on the same column again, the sort order will be toggled. 

  (***) 

  Dieses Beispiel zeigt die Verwendung der CustomSort Methode einer TListView. 
  Damit kann man z.B Zahlen korrekt (d.h nach ihrer Grosse) sortieren lassen. 
  Wenn der Anwender auf den Kopf einer Spalte klickt, wird die ensprechende 
  Spalte sortiert. Bei nochmaligem Klicken auf dieselbe Spalte wird die 
  Sortierreihenfolge umgekehrt. 

}

 { custom sort styles }

 type
   TCustomSortStyle = (cssAlphaNum, cssNumeric, cssDateTime);

 var
   { variable to hold the sort style }
   LvSortStyle: TCustomSortStyle;
   { array to hold the sort order }
   LvSortOrder: array[0..4] of Boolean; // High[LvSortOrder] = Number of Lv Columns 

implementation

 {$R *.DFM}

 function CustomSortProc(Item1, Item2: TListItem; SortColumn: Integer): Integer; stdcall;
 var
   s1, s2: string;
   i1, i2: Integer;
   r1, r2: Boolean;
   d1, d2: TDateTime;

   { Helper functions }

   function IsValidNumber(AString : string; var AInteger : Integer): Boolean;
   var
     Code: Integer;
   begin
     Val(AString, AInteger, Code);
     Result := (Code = 0);
   end;

   function IsValidDate(AString : string; var ADateTime : TDateTime): Boolean;
   begin
     Result := True;
     try
       ADateTime := StrToDateTime(AString);
     except
       ADateTime := 0;
       Result := False;
     end;
   end;

   function CompareDates(dt1, dt2: TDateTime): Integer;
   begin
     if (dt1 > dt2) then Result := 1
     else
       if (dt1 = dt2) then Result := 0
     else
       Result := -1;
   end;

   function CompareNumeric(AInt1, AInt2: Integer): Integer;
   begin
     if AInt1 > AInt2 then Result := 1
     else
       if AInt1 = AInt2 then Result := 0
     else
       Result := -1;
   end;

 begin
   Result := 0;

   if (Item1 = nil) or (Item2 = nil) then Exit;

   case SortColumn of
     -1 :
     { Compare Captions }
     begin
       s1 := Item1.Caption;
       s2 := Item2.Caption;
     end;
     else
     { Compare Subitems }
     begin
       s1 := '';
       s2 := '';
       { Check Range }
       if (SortColumn < Item1.SubItems.Count) then
         s1 := Item1.SubItems[SortColumn];
       if (SortColumn < Item2.SubItems.Count) then
         s2 := Item2.SubItems[SortColumn]
     end;
   end;

   { Sort styles }

   case LvSortStyle of
     cssAlphaNum : Result := lstrcmp(PChar(s1), PChar(s2));
     cssNumeric  : begin
                     r1 := IsValidNumber(s1, i1);
                     r2 := IsValidNumber(s2, i2);
                     Result := ord(r1 or r2);
                     if Result <> 0 then
                       Result := CompareNumeric(i2, i1);
                   end;
     cssDateTime : begin
                     r1 := IsValidDate(s1, d1);
                     r2 := IsValidDate(s2, d2);
                     Result := ord(r1 or r2);
                     if Result <> 0 then
                       Result := CompareDates(d1, d2);
                   end;
   end;

   { Sort direction }

   if LvSortOrder[SortColumn + 1] then
     Result := - Result;
 end;


 { The ListView's OnColumnClick event }

 procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
 begin
   { determine the sort style }
   if Column.Index = 0 then
     LvSortStyle := cssAlphaNum
   else
     LvSortStyle := cssNumeric;

   { Call the CustomSort method }
   ListView1.CustomSort(@CustomSortProc, Column.Index -1);

   { Set the sort order for the column}
   LvSortOrder[Column.Index] := not LvSortOrder[Column.Index];
 end;




Похожие по теме исходники

Сортировка методом Хоара

Метод Рунге-Кутта решения дифур

Метод Симпсона




Copyright © 2004-2024 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте