#1 2015-06-09 16:42:48

Junior/RO
Member
Registered: 2011-05-13
Posts: 210

Finding DynArray items

Ab, I am working with TDynArray with Delphi 7.

Everything is OK, but now I need to change the way to find items in the list.

TEspecie = record
  EspecieID: Integer;
  Nome: string;
  .. some other fields..
end;

When I am building the list, I need to search for the 'Nome' field. After, I will need to search by the 'EspecieID' field.

How to change Find() / FindAndFill() to work like this?

Thank you in advance.

Offline

#2 2015-06-09 19:09:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Finding DynArray items

Use your own comparison function at TDynArray initialization.

Offline

#3 2015-06-10 13:05:07

Junior/RO
Member
Registered: 2011-05-13
Posts: 210

Re: Finding DynArray items

Thank you.

Another question: just to sort a already existing array, how to do? If I try to init a TDynArray to a existing array, I can't sort. This code gives me an AV when call AList.Sort.

var
  Especies: TEspecieArray;
begin
  Especies := GetAllEspecies;
  AList.Init(TypeInfo(TEspecieArray), Especies);
  AList.Compare := MyCompareItem;
  AList.Sort;
end;

My workaround is create a new array, call AList.AddArray() and then Sort. There are a better approach?

function SortEspecies: TEspecieArray;
var
  SortedEspecies: TEspecieArray;
  Especies: TEspecieArray;
begin
  Especies := GetAllEspecies;

  AList.Init(TypeInfo(TEspecieArray), SortedEspecies);
  AList.AddArray(Especies);
  AList.Compare := MyCompareItem;
  ALista.Sort;

  Result := SortedEspecies;
end;

Offline

#4 2015-06-10 14:45:39

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Finding DynArray items

  AList.Init(TypeInfo(TEspecieArray), Especies);
  AList.Compare := MyCompareItem;
  AList.Sort;

should work directly.

I tried to reproduce the issue - see http://synopse.info/fossil/info/b72e1534e4
But no problem occurred.

What is the exact issue on your side in the Sort method?
How is your MyCompareItem() function implementation?

Offline

#5 2015-06-10 16:24:08

Junior/RO
Member
Registered: 2011-05-13
Posts: 210

Re: Finding DynArray items

This is weird. I tried again and it worked now.

Thank you again.

Offline

Board footer

Powered by FluxBB