How to sort a struct?

Hello!

I’ve created a highscore struct that stores the player name and their score but I want to display it from the biggest to the smallest.

I know how to do this with a normal array of int’s but not with a struct.

Can anyone help?

There aren’t too many good array sorting functions in Blueprint, however there are many ways to achieve this yourself.

The simplest way logically to do this is to create a second array of structs which represents the ‘sorted array’. Then run through a for each loop on your original array. In this for each loop, run a “for each loop with break” on this second ‘sorted’ array. In the second for each loop, check every element to see if your high score property is less than the element you have in your first for loop. If it is, then insert the array element you have from the first for each loop node into the array at the index of the array index output from the second for loop, and break the second loop. If this second for loop node completes without finding a smaller high score, then simply add the array element from the first for loop to the end of the second ‘sorted’ array.

This will result in logic that will stack your structs in order from highest to lowest score and the result of this sort will be in this second array. You can then assign the first array with the value of this sorted array.

Hi there.

Thanks for the answer but I can’t picture this in my head. Would you be able to knock something up so I can understand the process a little better?

Thanks

Hi,
this is my blueprint , I want auto sort by date in blueprint.
and when I add new section will move it to top, not bottom.
how can I do this ?
Thank you.

log:
label: log
type: structure
entry: >
{{date}} {{time}}

  {{text}}
fields:
  date:
    label: date
    type: date
    format: YYYY-mm-dd
  time:
    label: time
    type: time
  text:
    label: text
    required: true
    type:  markdown

I would also like to see a visual answer to this question.

I have an array of structs and want to reorder the array by one of the variables within the struct.

I’ve spent a few hours tonight figuring this out, and I thought I’d post it here for anyone else who comes across this thread.
I made a function that performs a [selection sort][1] on a struct array; in my case to sort a leaderboard array by score.

https://i.imgur.com/wgSKmHt.png

https://i.imgur.com/CO47JC1.png

https://i.imgur.com/bTVun3s.png

https://i.imgur.com/tNmqkdh.png

https://i.imgur.com/RmC8v95.png

And here is the macro I used to swap the array elements:

https://i.imgur.com/yZRiZdv.png

1 Like

http://puu.sh/muLIP/90d79d02ef.png

This is what I did for mine

http://puu.sh/muLIP/90d79d02ef.png

Hah, just came across this post as I needed this exact thing. Then I realised the post was from myself back in January.

Thanks past me!

It’s also worth mentioning that Rama’s Victory Plugin has a Sort node for arrays, and an option to sort by structure or object field. Works fine for me.

(copied my answer from another thread to this one too, for future generations =) )

maybe at one point that worked, but I’m here because the sort function from the victory plugin isn’t doing anything at all. That or it isn’t setting the array to the newly sorted one.

found out why it isn’t working, the function is not looking for the short name I assigned the value in the Struct, “Ammo” is the name. Instead it is looking for Ammo_5_19145E324396439F5c10A6941E12EBAC I’m not even sure how I would obtain that from a node if I didn’t happen to hover over a populated array and notice the contents were that.

Yes, it stopped working at some point, I even reported it in Rama’s thread. I’ve modified the code to sort arrays by struct variable name’s beginning, that did it for me, and I didn’t have time to work on an ultimate solution since then.
I guess I’ll remove my top comment if you don’t have anything to add.

You should use C++'s standard sort function, std::sort, declared in the header.

When you sort using a custom sorting function, you have to provide a predicate function that says whether the left-hand value is less than the right-hand value. So if you want to sort by name first, then by ID, then by amount due, all in ascending order, you could do:

bool customer_sorter(Customer const& lhs, Customer const& rhs) {
if (lhs.Name != rhs.Name)
return lhs.Name < rhs.Name;
if (lhs.Id != rhs.Id)
return lhs.Id < rhs.Id;
return lhs.AmountDue < rhs.AmountDue;
}
Now, pass that function to your sort call:

std::sort(customers.begin(), customers.end(), &customer_sorter);
This assumes you have an STL container (and not an array, like you have in your sample code) called customers containing customers.

Your Regards
Mirko Bronzi
Online Educational Instructor at Skillqore