0

How does the method Sort of the class TList work? Does this method only sort in a way that the elements of the list only ascend/descend? Please, have a look at the code below.

Type 
  PInteger = ^Integer;
Function Compare(Item1, Item2 : Pointer) : Integer;
Begin
 if PInteger(Item1)^ > Pinteger(Item2)^ then Result:= 1
 else if PInteger(Item1)^ < PInteger(Item2)^ then Result:= -1
 else Result:= 0;

End;
 { And, for instance, somewhere we call the method }
 List.Sort(Compare);

Now the thing is, after i compile the code, it works well, the list is sorted in a way that the elements ascend. But i don't understand the following line:

PInteger(item1)^ // What does this represent?

And what item1, item2 pointers point to? Do they not need to be initialized?

5
  • The code in Sort calls your compare function. Its job is to pass pointers to the elements. Your job is to return a value defining the order of the two items at the supplied addresses. Commented Dec 27, 2017 at 17:20
  • I removed the unnecessary prefix to your question - Stack Overflow already does an excellent job with the delphi tag alone. Commented Dec 27, 2017 at 17:26
  • ^ Thank you very much. But could you please clarify a certain thing? What do item1, item2 point to? And what does PInteger(Item1)^ mean? I debugged this code and noticed that the function compare(the argument) returns a value more than once? Why? Thanks in advance! Commented Dec 27, 2017 at 17:30
  • PInteger(item1)^ casts the pointer item1 to a typed pointer (i.e PInteger) then dereference it (i.e use the value stored in the address item1). Commented Dec 27, 2017 at 17:42
  • 1
    Part of the explanation is going to lie in how you populated the list. You didn't show that. Commented Dec 27, 2017 at 18:09

1 Answer 1

3

First what PInteger(item1)^ does/represent?

  1. Item1 is a Pointer, the address of an item stored in the TPointerList.
  2. PInteger is a typed pointer, this means that this pointer points to an address where it is expected to find an integer (four bytes).
  3. ^ the dereferencing symbol, you can use this with pointers to tell the compiler that you want to use the data stored in the address that the pointer is currently pointing to
  4. PInteger(item1)^ you are performing a typecast. in other words you are telling the compiler to treat the pointer Item1 as if it was PInteger then you dereference it to use its data/value stored at the address Item1

Now back to your code. Your function expects two pointers to Items (Integers) from the list which then you compare the data stored in those address (by dereferencing). This means that the list is responsible for the pointers given to your function, in fact if the ItemCount is less than 1 your function will not be executed.

Note: you need to understand that this function will fail if the pointers are pointing to something other than integers (or give an undefined behavior).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Your answer has been a great help. Only one more thing. I want to understand if the method Sort understands on its own that item1 and item2 are pointers from the list? I mean we don't need to initialise them somehow? ( Does that make any sense? )
@ArthurHmayakyan When you populate the list you are giving it a pointer to your items which will be stored by the list and used later when you call .Sort(Compare), so there is no need to initialize the items and yes the pointers are from the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.