#1 2026-03-03 08:17:26

larand54
Member
Registered: 2018-12-25
Posts: 131

How can I sort an IList?

I Have an an old sorted stringlist that that I need to replace with an IList:
The constructor looks like this:

constructor TCtrlV2List.create;
var
  comparer : IComparer<TSQLLobUserControllv2>;
  comparison : TComparison<TSQLLobUserControllv2>;
begin
  inherited create;
  comparison := function(const l,r : TSQLLobUserControllv2):integer
                  begin
                    if (l.LOBuffertNo = r.LOBuffertNo) and (l.PkgArticleNo = r.PkgArticleNo) and (l.UserID = r.UserID) then exit(0)    // This item already exist in the list!
                    else if l.LOBufferNo > r.LOBufferNo then exit(-1)
                    else if (l.LOBufferNo = r.LOBufferNo) and (l.PkgArticleNo > r.PkgArticleNo) then exit(-1)
                    else if (l.LOBufferNo = r.LOBufferNo) and (l.PkgArticleNo = r.PkgArticleNo) and (l.UserID > r.UserID) then exit(-1)
                    else exit(1);
                  end;

  comparer := TComparer<TSQLLobUserControllv2>.Construct(comparison);
  inherited Create(comparer);
end;

Now I wonder how I should do to accomplish this with an IList?

Last edited by larand54 (2026-03-03 08:18:11)


Delphi-11, WIN10 and Win11

Offline

#2 2026-03-03 10:17:55

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,413
Website

Re: How can I sort an IList?

What is wrong with IList.Sort() ?

You just have to define a local TDynArraySortCompare function.

function SortByLO(const a,b):integer
var l,r : TSQLLobUserControllv2;
begin
  l := pointer(a);
  r := pointer(b);
  if (l.LOBuffertNo = r.LOBuffertNo) and (l.PkgArticleNo = r.PkgArticleNo) and (l.UserID = r.UserID) then exit(0)    // This item already exist in the list!
    else if l.LOBufferNo > r.LOBufferNo then exit(-1)
    else if (l.LOBufferNo = r.LOBufferNo) and (l.PkgArticleNo > r.PkgArticleNo) then exit(-1)
    else if (l.LOBufferNo = r.LOBufferNo) and (l.PkgArticleNo = r.PkgArticleNo) and (l.UserID > r.UserID) then exit(-1)
    else exit(1);
end;

and by the way your code seems incorrect, you are calling Create() twice.

Offline

#3 2026-03-03 10:45:54

larand54
Member
Registered: 2018-12-25
Posts: 131

Re: How can I sort an IList?

Thank's a lot, I feel  a bit stupid but I have to ask - How should I connect this function to the list?

And to the comment  of the second call to create - I don't know why this extra call has been placed there, I think it should have replaced the first call to create.
It's been a long time since I wrote this code and it has been running 24h a day in several years without any problem that I have seen.


Delphi-11, WIN10 and Win11

Offline

#4 2026-03-03 10:55:27

zed
Member
Registered: 2015-02-26
Posts: 117

Re: How can I sort an IList?

larand54 wrote:

Thank's a lot, I feel  a bit stupid but I have to ask - How should I connect this function to the list?

Just read the comments carefully:

https://github.com/synopse/mORMot2/blob … s.pas#L173

https://github.com/synopse/mORMot2/blob … s.pas#L290

Last edited by zed (2026-03-03 11:01:24)

Offline

#5 2026-03-03 11:11:59

larand54
Member
Registered: 2018-12-25
Posts: 131

Re: How can I sort an IList?

Thanks zed, but I think I solved it already. I wrote -

UctrlList.Sort(@SortByLO);

and that seemed to work fine.


Delphi-11, WIN10 and Win11

Offline

Board footer

Powered by FluxBB