Recent

Author Topic: [SOLVED] Is there a way to sort a TLisistBox desc?  (Read 3683 times)

ravida

  • Newbie
  • Posts: 1
Re: Is there a way to sort a TLisistBox desc?
« Reply #15 on: June 28, 2023, 01:57:45 pm »
No, the TListBox component in Delphi does not have a built-in way to sort its elements in descending order. By default, it sorts the elements in ascending order. If you want to display the elements in descending order, you have to sort the elements manually before populating the TListBox.

Here is a general approach you can take:

Create a TStringList object.
Paste all the items from the TListBox into the TStringList.
Use the CustomSort method of the TStringList and provide a custom comparison function that sorts the items in descending order.
Delete the TListBox.
Put the sorted elements from the TStringList back into the TListBox.
Here is a sample code snippet:

var
  ListBoxItems: TStringList;
  I: Integer;
begin
  ListBoxItems := TStringList.Create;
  try
    // Add items from TListBox to TStringList
    ListBoxItems.Assign(ListBox1.Items);

    // Custom sort in descending order
    ListBoxItems.CustomSort(
      function(const S1, S2: string): Integer
      begin
        Result := -CompareStr(S1, S2);
      end
    );

    // Clear the TListBox
    ListBox1.Clear;

    // Add sorted items back to TListBox
    for I := 0 to ListBoxItems.Count - 1 do
      ListBox1.Items.Add(ListBoxItems);
  finally
    ListBoxItems.Free;
  end;
end;


Note that this procedure assumes that you are using the VCL framework in Delphi. If you are using a different framework or a third-party component, the specific steps and code may differ.

thanks and regards
pawan tanwar
« Last Edit: July 14, 2023, 10:35:34 am by marcov »

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Is there a way to sort a TLisistBox desc?
« Reply #16 on: June 28, 2023, 02:07:13 pm »
My posting in short: Are there anywhere easy-to-understand links which explain "class methods" and "static"?
and "class helper" is one more.
I'm sure you already came across the links I post but I do believe they are written in an understandable manner ?
- class methods
- static class methods
- and though not authoritative (because wiki) but very easy to read: class helpers (or actually helpers in general).

@ravida:
Yeah introduce anonymous functions, a perfect idea to let people install trunk when you consider the question(s).
« Last Edit: June 28, 2023, 02:28:20 pm by TRon »
Today is tomorrow's yesterday.

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: Is there a way to sort a TLisistBox desc?
« Reply #17 on: June 28, 2023, 02:54:20 pm »
Listbox items are of type TStrings, so you can call reverse on it and make a copy, like so:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses classes;
  3. const sa:array of string = ('a','b','c');
  4. var L1,L2:Tstrings;
  5. begin
  6.    L1:=TStringlist.Create;
  7.    L2:=TStringlist.Create;
  8.    try
  9.      L1.Addstrings(sa); // This can be a TListbox.Items
  10.      L1.Reverse(L2); // target is L2
  11.      writeln(L2.Text);// inverse of L1, assign this to TListBox.Items
  12.    finally
  13.      L1.Free;
  14.      L2.free;
  15.    end;  
  16. end.
Subsequently assign the new Tstrings (L2) to the list box. Nothing fancy.
Although there are two caveats: if you call the reverse procedure on itself, the reversed list will be added, so you need to have a separate TStrings instance, use the procedure reverse to contain the reversed order, and the assign it!.
The reverse function leaks memory, but the reverse procedure does not leak. I will report a bug for the function.

More info: https://www.freepascal.org/docs-html/rtl/classes/tstrings.reverse.html
« Last Edit: June 28, 2023, 03:09:03 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018