Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Sorting a TArray of structs

Hi all,
hope you guys can give me a hand with this.
Let's imagine I have a TArray of 10 structs:

 struct testStruct { int32 a; int32 b; int32 c }

Its content is for example this (first column are indexes):

 0: 5, 4, 5
 1: 1, 4, 5
 2: 5, 4, 7
 3: 7, 1, 5
 4: 7, 2, 5
 5: 3, 6, 7
 6: 2, 4, 5
 7: 1, 1, 7
 8: 4, 1, 7
 9: 6, 2, 7

How would I sort it on a, so to have this output?

 0: 1, 4, 5
 1: 1, 1, 7
 2: 2, 4, 5
 3: 3, 6, 7
 4: 4, 1, 7
 5: 5, 4, 5
 6: 5, 4, 7
 7: 6, 2, 7
 8: 7, 1, 5
 9: 7, 2, 5

Thanks in advance!

Product Version: UE 4.10
more ▼

asked Mar 07 '16 at 09:20 PM in C++ Programming

avatar image

226 23 20 26

avatar image rantrod Mar 07 '16 at 09:29 PM

This suspiciously looks like a homework assignment, but look at defining a comparison operator for your struct.

avatar image ClaudioPiccinni Mar 08 '16 at 09:32 AM

Yeah, I wish. ;)
I'm looking into that, thanks.

avatar image ClaudioPiccinni Mar 08 '16 at 10:58 AM

Well, I was going through a rabbit hole here... I tried to transform my c++ structs into USTRUCTs and failed (haven't understood how to declare them in my header... sigh), then I tried to override the operator in the classic structs, and failed too.

In the end I solved by just implementing a bubble sort in my function like this:

 // where testStructList is a TArray<testStruct *>
 int32 iterations = testStructList .Num() - 1;
 while (iterations > 0)
     for (int32 x = 0; x < testStructList .Num() - 1; x++)
         if (testStructList [x]->a > testStructList [x + 1]->a)
             testStructList .Swap(x, x + 1);

avatar image rantrod Mar 08 '16 at 11:06 PM

your code only sorts the 'a' field. You need 'b' and 'c' accounted for. If the a's are equal then do the same test on b. If the a's and b's are equal, then do the same test on 'c'.

(comments are locked)
10|2000 characters needed characters left

3 answers: sort voted first


You can sort by using TArray's Sort member function and a lambda (C++11 feature) which describes how your elements are to be sorted. If you want to sort by a, then b, then c, then you should have something like:

 Array.Sort([](const testStruct& Lhs, const testStruct& Rhs) -> bool {
     // sort by a
     if (Lhs.a < Rhs.a) return true; 
     if (Lhs.a > Rhs.a) return false;

     // sort by b, but only if 'a's were equal
     if (Lhs.b < Rhs.b) return true; 
     if (Lhs.b > Rhs.b) return false;

     // sort by c, but only if 'a's and 'b's were equal
     return Lhs.c < Rhs.c;

Hope this helps,


more ▼

answered Mar 08 '16 at 04:19 PM

avatar image

Steve Robb STAFF
2.7k 58 25 99

avatar image Steve Robb STAFF Mar 08 '16 at 04:26 PM

Or if you just want to sort by a and keep structs with equal 'a's in the same relative order, then you can use:

 Array.StableSort([](const testStruct& Lhs, const testStruct& Rhs) {
     return Lhs.a < Rhs.b;


avatar image ComposingCoder Feb 03 '19 at 03:18 AM

DUDE! You just saved me sooo much time figuring out how to sort my struct for my targeting component lol Thanks! (Struct just contains ACharacter* Target, FString TargetName, and float TargetDistance). I've been trying to figure out how to sort by distance lol.

avatar image Steve Robb STAFF Feb 04 '19 at 08:07 AM

It should be noted that since my earlier reply, we now have a more convenient algorithm for this kind of thing:

 Algo::SortBy(ArrayOfMyStruct, &MyStruct::TargetDistance);


(comments are locked)
10|2000 characters needed characters left

TArray has a function for sorting


In order to work with your struct or class you need to override > operator that should declere if inputed A is bigger the B, then soft function to the rest with help of that operator

more ▼

answered Mar 07 '16 at 11:53 PM

avatar image

34.5k 917 164 1094

avatar image ClaudioPiccinni Mar 08 '16 at 09:56 AM

hey Shadowriver,
can it be done with normal structs, or should I make my structs into USTRUCTs? Could you show me any examples? This is still uncharted territory for me :p

avatar image Shadowriver Mar 08 '16 at 05:06 PM

Anything that support > operator and in struct you can override it

(comments are locked)
10|2000 characters needed characters left

There might be an easier way, but the way I've done it in the past is create a class that overloads operator(). It would look something like

 class Sorter
     bool operator()(const testStruct& x, const textStruct& y) const;

Implement your comparison however you want in that function and just pass in instance of the Sorter class to TArray::Sort.

more ▼

answered Mar 07 '16 at 10:59 PM

avatar image

904 39 56 200

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question