Download

Ordering an array of actors based on an Int set inside the actor...

Hi all,

I’m trying to figure out how I can set the order of an array of objects (actor BPs) based on an Int set on the object. I’ve found examples of taking an array of Ints and using the Min or Max node, but how would I do this if the array I have is not the info I’m trying to sort? Is this even possible? I’ve thought about pushing the array to a new array in the same order, sorting that and then re-setting the original array in that order, but how can I make sure that my indexes match up correctly doing that?

Basic setup: Playing Card BP (based on how Black Jack example project did cards), has a variable Int that denotes the order of the card in its suit. I need to look at 5 cards in an array and order them from high to low (or reverse, not sure what’s best yet). I’m trying to do this to be able to properly score hands of Cribbage if that helps anyone. Here’s the general idea I’m working off of:

Look towards the bottom on the Runs scoring. I’ve gotten everything else to work. If anyone has a better way to do this, I’m all ears (and eyes if you have examples that would help a lot).

Thanks.

I’m actually surprised Unreal doesn’t have array sort functionality already built in. It’s part of the standard function libraries to C, C++, and C#. But I’m a little confused by your question and I’m seeking some clarification. I’m reading this initially as you having an array “A” of objects of type “O” where each of those said objects have a member component of integer “I” and wanting to sort on that integer, but then you say “but how would I do this if the array I have is not the info I’m trying to sort?” What are you trying to sort? As long as you can access a member of an arbitrary item in an array you can sort that array based on that member’s value. And you can do the sort in place on the existing array without having to create an additional buffer array to do the sorting. You can use the Set Array Elem node for that. You will need a variable of the same type of the array items as a buffer but that’s all the extra memory requirement you’ll need.

Not totally sure I understand your response (and a little tipsy, TBH), but I’ll try and clarify. I have a Blueprint that is a playing card. In this BP, I have value, number, order and suit set as variables. These are set when the card is created (spawned). When I start a game, I create cards into “hands” which are just arrays catching those cards for each player. I want to sort these cards within that hand by the ‘Order’ variable set on it and then perform various actions on the array in that new sorted order. I’m trying to compare the 5 cards to each other to determine if there is a run of 3, 4 or 5 cards.

I’m not sure I understand what you mean by using Set Array Elem. I’ve used this, but thought it was intended for manual array manipulation. I’m looking for something slightly automated to quickly compare values and move things around.

bcb76b62fab50adb4c672285d450764db040a28f.jpeg

Didn’t test it, but should work.

looks terrible ^

I don’t see a comparison operator there. Your model assumes the integers are sequential and start zero. But that is a good example of an insertion sort. You didn’t save your result back into the original array though.

here, my take https://drive.google.com/file/d/0B9-wGP04rKFmbnVxUjhCSGRnZFE/view?usp=sharing
dosnt looks good, but works.

Thanks for the help all. I ended up getting some help off a Facebook group and got it working. Here’s what I used. Thanks to Jouni Laitinen for the help.

Jouni Laitinen Meh, I wanted to share this through http://blueprintue.com/ but I guess it doesn’t really support functions… so here’s some ugly images instead grin emoticon

&stc=1

&stc=1

FYI: This is a VERY ugly sort algorithm that always uses the “worst case” time complexity of n^2. It’ll work well enough for small arrays, but even as a casual programmer it makes me sick to post such an inefficient algorithm grin emoticon

Basically in the first pic I’m just making an array of “structs” (for which you’ll obviously use array of your Actors instead) and then I just pass it into the ForLoop and Ugly Sort function.

The 2nd picture is the Ugly Sort function, which basically iterates through the entire array, pulls the ‘integer’ variable from the struct (in your case you’ll pull them from the Actor value), compare the two and if other is smaller than the other, swap their positions.

Repeated N times, this will fully sort the array in a very ineffective and horrible way that nobody should ever use grin emoticon

Also… spaghetti horror. This is why I don’t make complicated things in BP grin emoticon

For more effective sorting, look into something likehttps://en.wikipedia.org/wiki/Quicksort and try to implement that in BP. It’s definitely possible, but it won’t be very pretty grin emoticon

pics broken for me