Sorting TSharedPtr<FString> not working

Hey,

Just had a quick question that is probable really easy to solve but I must be too blind to see the obvious.

Im simply said trying to sort a TArray of TSharedPtr< FString >.
But it doesnt work.

Options.Sort() just crashes telling me:

binary ‘<’: no operator found which takes a left-hand operand of type ‘const T’ (or there is no acceptable conversion)


And with a predicate it also crashes:

Options.Sort([](const TSharedPtr<FString>& A, const TSharedPtr<FString>& B) 
		{
			return A < B;
		})

binary ‘<’: no operator found which takes a left-hand operand of type ‘const TSharedPtr<FString,ESPMode::NotThreadSafe>’ (or there is no acceptable conversion)

Not exactly sure what to do about that, would be great if someone could help me out! :slightly_smiling_face:

I think you need to dereference the pointer before using FString operators on it, is there any reason for you to use a TSharedPtr on a FString? If you use Slate and need to manage memory manually I would recommend to wrap a TArray< FString> with TSharedPtr instead

Yes I am using it with Slate for a dropdown.
Which unfortunately requires me to use TArray<TSharedPtr< FString>> for the .OptionsSource(), as it doesn’t have any overloads that accept it otherwise to my knowledge.

Unless im missing something I dont know that I could give it anything else.

You can try pulling actual FStrings from your shared pointers and using them as predicates. < can compare strings, but can’t compare pointers.

2 Likes

Right yes, that was very obvious just like I though. I was just too blinded by the errors to actually see that.
Thanks!

(For anyone that wants to know in the future it was simply this: return *A < *B; )