Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to sort the items contained in a TComboBox using TFmxObjectSortCompare?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Jan 21, 2016 9:23 AM Last Post By: John Murray
John Murray

Posts: 12
Registered: 1/22/16
How to sort the items contained in a TComboBox using TFmxObjectSortCompare?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 21, 2016 7:44 AM
I'm currently trying to sort the items in a TComboBox using the Sort function which requires that I pass through a function that takes in two TFmxObject pointers and returns an int.

I'm not sure how to actually implement the custom sort function.

In VCL, I was able to put the function outside a class and pass it through to Sort just fine but I'm not sure how to do the same with Firemonkey.
I get the following error:
[bcc32 Error] ToolView.cpp(89): E2034 Cannot convert 'int(TFmxObject *,TFmxObject *)' to '_di_TFmxObjectSortCompare'


The documentation is lacking for this unless I'm somehow missing something
http://docwiki.embarcadero.com/Libraries/XE7/en/FMX.Types.TFmxObjectSortCompare
John Murray

Posts: 12
Registered: 1/22/16
Re: How to sort the items contained in a TComboBox using TFmxObjectSortCompare?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 21, 2016 9:12 AM   in response to: John Murray in response to: John Murray
For anyone who may come across this in the future, I had to implement the sort function using a template and a struct.

#include <System.hpp>
 
enum _DummyType {
}; // Parameter used as default
 
template<typename INTF, // Interface with Invoke
    typename Functor, // Functor type
    typename Return, // Return type
    typename P1 = _DummyType, // Param #1
    typename P2 = _DummyType> // Param #2
class CustomSort : public TCppInterfacedObject<INTF> {
private:
    Functor callback;
 
public:
    CustomSort(Functor _callback) :
        callback(_callback) {
    }
    INTFOBJECT_IMPL_IUNKNOWN(TInterfacedObject);
 
    Return __fastcall Invoke(P1 p1, P2 p2) {
        return callback(p1, p2);
    }
};
 
struct CustomComboBoxSortFunction {
    CustomComboBoxSortFunction() {}
 
    int operator()(TFmxObject* LeftItem, TFmxObject* RightItem) {
        UnicodeString left = (reinterpret_cast<TListBoxItem *>(LeftItem))->Text;
        UnicodeString right = (reinterpret_cast<TListBoxItem *>(RightItem))->Text;
 
        if (left.Length() < right.Length()) {
            return -1;
        }
        if (left.Length() > right.Length()) {
            return 1;
        }
 
        int test = CompareText(left, right);
        return (test);
    };
};
 
typedef CustomSort<TFmxObjectSortCompare, CustomComboBoxSortFunction, int, TFmxObject *, TFmxObject *>CCustomSort;


(Info from http://docwiki.embarcadero.com/RADStudio/XE8/en/How_to_Handle_Delphi_Anonymous_Methods_in_C%2B%2B)

Then call the Sort function with the following
 ComboBox->Sort(_di_TFmxObjectSortCompare(new CCustomSort(CustomComboBoxSortFunction())));

(Remember to include the header that the template/struct/typedef are in and also set the Sort function BEFORE adding any items).

EDIT: Weirdly, it appears to need to be used before and after adding items.

Edited by: John Murray on Jan 21, 2016 5:23 PM

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02