33

Say I have an array of records which I want to sort based on one of the fields in the record. What's the best way to achieve this?

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

var SomeVar : array of TExample;
menjaraz
  • 7,551
  • 4
  • 41
  • 81
Marius
  • 2,494
  • 6
  • 31
  • 41
  • Just went through this exercise and found the best way is to write my own code. I don't think any of the answers should be recommended as **best**. – Sam Jul 22 '11 at 05:07
  • Point taken. Maybe you could add an answer with your solution to the problem as well? – Marius Jul 22 '11 at 10:41
  • There is some good information in Tomes of Delphi Algorithms and Data Structures by Julian Bucknall. (s – Richard A Mar 21 '12 at 01:46
  • 1
    There is some good information in Tomes of Delphi Algorithms and Data Structures. (Here, amongst other places: http://blog.boyet.com/blog/blog/tomes-of-delphi-algorithms-and-data-structures-kindle-edition/) – Richard A Mar 21 '12 at 01:46

10 Answers10

40

You can add pointers to the elements of the array to a TList, then call TList.Sort with a comparison function, and finally create a new array and copy the values out of the TList in the desired order.

However, if you're using the next version, D2009, there is a new collections library which can sort arrays. It takes an optional IComparer<TExample> implementation for custom sorting orders. Here it is in action for your specific case:

TArray.Sort<TExample>(SomeVar , TDelegatedComparer<TExample>.Construct(
  function(const Left, Right: TExample): Integer
  begin
    Result := TComparer<Integer>.Default.Compare(Left.SortOrder, Right.SortOrder);
  end));
Sid M
  • 4,354
  • 4
  • 30
  • 50
Barry Kelly
  • 41,404
  • 5
  • 117
  • 189
  • 1
    Note that in D2009 there are multiple issues in the compiler with Generics, so using the collections library is never a good option (because of spurious compiler internal errors and codegen errors). In Delphi XE those issues have been solved for the most part, though using the generics versions will involve a performance hit (and you may lose some code clarity/debuggability). – Eric Grange Nov 02 '10 at 07:29
  • 3
    It's one thing to solve a problem. It's another to write an elegant solution (meaning "easy on the eyes"). I don't think this code looks good or is readable enough. I'd hate for Delphi to slowly lose its readability, otherwise there'll be one less reason not to switch to C# as my preferred language. – Sam Jul 22 '11 at 04:55
  • 4
    This solution is compact and terse because it reuses a lot of commonly built code and allows an anonymous function to be written inline rather than forcing you to create a dummy function declaration just so you can pass it to this one function. You can rewrite all this code yourself and take up a page or two or three. Or you can do it in five lines. I think it's a bit debatable whether the longer version would be more "readable" than the shorter one. – jep Jun 24 '13 at 18:36
  • @jep Part of the problem is that the anonymous function syntax is so verbose. Putting an entire function in the middle of another function is simply far less readable. Second, if we had lambdas and especially if our sorting used a key rather than a custom comparison function the entire enterprise would be one line. For instance, in Python the entire sort operation above would be sorted(someVar, key=lambda x: x.SortOrder)! Python doesn't allow a function larger than a one-line lambda inline precisely because it results in unreadable code. We could really use lambdas and key sorts. – alcalde Sep 02 '13 at 18:39
11

(I know this is a year later, but still useful stuff.)

Skamradt's suggestion to pad integer values assumes you are going to sort using a string compare. This would be slow. Calling format() for each insert, slower still. Instead, you want to do an integer compare.

You start with a record type:

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

You didn't state how the records were stored, or how you wanted to access them once sorted. So let's assume you put them in a Dynamic Array:

var MyDA:  Array of TExample; 
...
  SetLength(MyDA,NewSize);           //allocate memory for the dynamic array
  for i:=0 to NewSize-1 do begin        //fill the array with records
    MyDA[i].SortOrder := SomeInteger;
    MyDA[i].SomethingElse := SomeString;
  end;

Now you want to sort this array by the integer value SortOrder. If what you want out is a TStringList (so you can use the ts.Find method) then you should add each string to the list and add the SortOrder as a pointer. Then sort on the pointer:

var  tsExamples: TStringList;         //declare it somewhere (global or local)
...
  tsExamples := tStringList.create;   //allocate it somewhere (and free it later!)
...
  tsExamples.Clear;                   //now let's use it
  tsExamples.sorted := False;         //don't want to sort after every add
  tsExamples.Capacity := High(MyDA)+1; //don't want to increase size with every add
                                      //an empty dynamic array has High() = -1
  for i:=0 to High(MyDA) do begin
    tsExamples.AddObject(MyDA[i].SomethingElse,TObject(MyDA[i].SortOrder));
  end;

Note the trick of casting the Integer SortOrder into a TObject pointer, which is stored in the TStringList.Object property. (This depends upon the fact that Integer and Pointer are the same size.) Somewhere we must define a function to compare the TObject pointers:

function CompareObjects(ts:tStringList; Item1,Item2: integer): Integer;
begin
  Result := CompareValue(Integer(ts.Objects[Item1]), Integer(ts.Objects[Item2]))
end;

Now, we can sort the tsList on .Object by calling .CustomSort instead of .Sort (which would sort on the string value.)

tsExamples.CustomSort(@CompareObjects);     //Sort the list

The TStringList is now sorted, so you can iterate over it from 0 to .Count-1 and read the strings in sorted order.

But suppose you didn't want a TStringList, just an array in sorted order. Or the records contain more data than just the one string in this example, and your sort order is more complex. You can skip the step of adding every string, and just add the array index as Items in a TList. Do everything above the same way, except use a TList instead of TStringList:

var Mlist: TList;                 //a list of Pointers
...
  for i:=0 to High(MyDA) do
    Mlist.add(Pointer(i));        //cast the array index as a Pointer
  Mlist.Sort(@CompareRecords);    //using the compare function below

function CompareRecords(Item1, Item2: Integer): Integer;
var i,j: integer;
begin
  i := integer(item1);            //recover the index into MyDA
  j := integer(item2);            // and use it to access any field
  Result := SomeFunctionOf(MyDA[i].SomeField) - SomeFunctionOf(MyDA[j].SomeField);
end;

Now that Mlist is sorted, use it as a lookup table to access the array in sorted order:

  for i:=0 to Mlist.Count-1 do begin
    Something := MyDA[integer(Mlist[i])].SomeField;
  end;

As i iterates over the TList, we get back the array indexes in sorted order. We just need to cast them back to integers, since the TList thinks they're pointers.

I like doing it this way, but you could also put real pointers to array elements in the TList by adding the Address of the array element instead of it's index. Then to use them you would cast them as pointers to TExample records. This is what Barry Kelly and CoolMagic said to do in their answers.

CaptureWiz
  • 1,685
  • 1
  • 15
  • 15
Guy Gordon
  • 740
  • 6
  • 13
3

If your need sorted by string then use sorted TStringList and add record by TString.AddObject(string, Pointer(int_val)).

But If need sort by integer field and string - use TObjectList and after adding all records call TObjectList.Sort with necessary sorted functions as parameter.

Sid M
  • 4,354
  • 4
  • 30
  • 50
CoolMagic
  • 258
  • 1
  • 2
  • 7
2

This all depends on the number of records you are sorting. If you are only sorting less than a few hundred then the other sort methods work fine, if you are going to be sorting more, then take a good look at the old trusty Turbo Power SysTools project. There is a very good sort algorithm included in the source. One that does a very good job sorting millions of records in a efficient manner.

If you are going to use the tStringList method of sorting a list of records, make sure that your integer is padded to the right before inserting it into the list. You can use the format('%.10d',[rec.sortorder]) to right align to 10 digits for example.

skamradt
  • 15,366
  • 2
  • 36
  • 53
2

The quicksort algorithm is often used when fast sorting is required. Delphi is (Or was) using it for List.Sort for example. Delphi List can be used to sort anything, but it is an heavyweight container, which is supposed to look like an array of pointers on structures. It is heavyweight even if we use tricks like Guy Gordon in this thread (Putting index or anything in place of pointers, or putting directly values if they are smaller than 32 bits): we need to construct a list and so on...

Consequently, an alternative to easily and fastly sort an array of struct might be to use qsort C runtime function from msvcrt.dll.

Here is a declaration that might be good (Warning: code portable on windows only).

type TComparatorFunction = function(lpItem1: Pointer; lpItem2: Pointer): Integer; cdecl;
procedure qsort(base: Pointer; num: Cardinal; size: Cardinal; lpComparatorFunction: TComparatorFunction) cdecl; external 'msvcrt.dll';

Full example here.

Notice that directly sorting the array of records can be slow if the records are big. In that case, sorting an array of pointer to the records can be faster (Somehow like List approach).

rt15
  • 87
  • 1
  • 1
  • 7
1

With an array, I'd use either quicksort or possibly heapsort, and just change the comparison to use TExample.SortOrder, the swap part is still going to just act on the array and swap pointers. If the array is very large then you may want a linked list structure if there's a lot of insertion and deletion.

C based routines, there are several here http://www.yendor.com/programming/sort/

Another site, but has pascal source http://www.dcc.uchile.cl/~rbaeza/handbook/sort_a.html

Sid M
  • 4,354
  • 4
  • 30
  • 50
ddowns
  • 316
  • 1
  • 5
  • 8
0

TStringList have efficient Sort Method.
If you want Sort use a TStringList object with Sorted property to True.

NOTE: For more speed, add objects in a not Sorted TStringList and at the end change the property to True.
NOTE: For sort by integer Field, convert to String.
NOTE: If there are duplicate values, this method not is Valid.

Regards.

0

Use one of the sort alorithms propose by Wikipedia. The Swap function should swap array elements using a temporary variable of the same type as the array elements. Use a stable sort if you want entries with the same SortOrder integer value to stay in the order they were in the first place.

Ralph M. Rickenbach
  • 12,893
  • 5
  • 29
  • 49
0

If you have Delphi XE2 or newer, you can try:

var 
  someVar: array of TExample;
  list: TList<TExample>;
  sortedVar: array of TExample;
begin
  list := TList<TExample>.Create(someVar);
  try
    list.Sort;
    sortedVar := list.ToArray;
  finally
    list.Free;
  end;
end;
Jacek Krawczyk
  • 2,083
  • 1
  • 19
  • 25
-1

I created a very simple example that works correctly if the sort field is a string.

Type
  THuman = Class
  Public
    Name: String;
    Age: Byte;
    Constructor Create(Name: String; Age: Integer);
  End;

Constructor THuman.Create(Name: String; Age: Integer);
Begin
  Self.Name:= Name;
  Self.Age:= Age;
End;

Procedure Test();
Var
 Human: THuman;
 Humans: Array Of THuman;
 List: TStringList;
Begin

 SetLength(Humans, 3);
 Humans[0]:= THuman.Create('David', 41);
 Humans[1]:= THuman.Create('Brian', 50);
 Humans[2]:= THuman.Create('Alex', 20);

 List:= TStringList.Create;
 List.AddObject(Humans[0].Name, TObject(Humans[0]));
 List.AddObject(Humans[1].Name, TObject(Humans[1]));
 List.AddObject(Humans[2].Name, TObject(Humans[2]));
 List.Sort;

 Human:= THuman(List.Objects[0]);
 Showmessage('The first person on the list is the human ' + Human.name + '!');

 List.Free;
End;
David Miró
  • 2,694
  • 20
  • 20