The Challenge of Sorting Arrays in Delphi

Sorting is a fundamental operation in programming that organizes data into a meaningful order. When working with arrays in Delphi—particularly arrays of records—this can pose a unique challenge. Imagine you have an array of records and wish to sort them based on a specific field, like an integer value representing a sort order. What’s the best approach to achieve this efficiently?

In this post, we’ll explore the best way to sort an array in Delphi, breaking down both traditional methods and the newer techniques introduced in later versions. Let’s dive in!

Traditional Approach: Using TList

One method to sort an array of records is by utilizing a dynamic list. Here’s how you can do that step by step:

Step 1: Create Your Record Type

First, you need to define the structure of your record. In this case, we will create a record type called TExample with a sort order integer and another field.

type
  TExample = record
    SortOrder: integer;
    SomethingElse: string;
  end;

Step 2: Declare Your Array

Next, declare an array that will hold instances of your record.

var
  SomeVar: array of TExample;

Step 3: Sort with TList

To sort your records, you can create a list and add pointers to the elements of your array. The TList class provides a built-in sorting method, but you’ll need to supply a comparison function to define how items should be compared.

New and Improved: Collections Library in D2009

If you’re using Delphi 2009 or later, there’s an even more streamlined approach provided by the new collections library. This method allows you to sort arrays directly and offers greater flexibility with custom sorting through an IComparer implementation.

Step 1: Use TArray.Sort

The new sorting method leverages the TArray.Sort function along with a custom comparer. Here’s how to implement it:

TArray.Sort<TExample>(SomeVar, TDelegatedComparer<TExample>.Construct(
  function(const Left, Right: TExample): Integer
  begin
    Result := TComparer<Integer>.Default.Compare(Left.SortOrder, Right.SortOrder);
  end));

How It Works

  • Comparison Function: You provide a comparison function where you dictate how two items should be compared. In our example, we’re comparing the SortOrder property of two TExample records.
  • Sorted in Place: Using TArray.Sort, your original array SomeVar will be sorted in place, meaning that no additional memory allocation for a new array is necessary, improving performance and efficiency.

Conclusion

Sorting arrays of records in Delphi can be done effectively using either traditional methods with TList or the modern approach introduced in Delphi 2009. By leveraging built-in tools like TArray.Sort and custom comparison functions, you can efficiently organize your data while keeping your code clean and maintainable.

Whether you opt for the classic approach or modern enhancements, understanding these techniques will empower you to manipulate and sort data as needed in your Delphi applications.