How can I sort items in array by properties?

How can I sort items in array by properties (for example item size or location)?
Thanks!

Use a ForEachLoop node.

Array Doc:
https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Arrays/ArrayNodes/index.html

You can use the “Filter Array” or “Remove Item” with ForEachLoop and drop them back into your array in the order of preference.

I have an array of “Item” objects (my own class)… “Item” objects have “Category” enum with values like “consumable”, “weapon”, etc. - and I can’t figure out how to sort it by Category. Maybe it’s easy, but i’ve used to have a built-in Sort() function and doing it by hand is quite new to me :slight_smile: I will be grateful for some advice or example of array sorting in Blueprint.

I too could use an example of how this would work.

https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Arrays/ArrayNodes/index.html

Use Filter to sort by class.

If “Item” is your parent class, and Weapon, Armor, and Consumeable are child classes, then you can filter all items, all weapons, all armor, etc.

You can also have children of the Weapon class and filter all Axes, Swords, etc.

I’m having the same problem. The answers mentioned here only help me filter for specific item types. I’d love to be able to resort the array (or build a new one) based on something more basic (like x-coord, scale, etc) like the original poster asked.

Do you mean something like an array of structs that have multiple entries that could be sorted on or just like a single array of vectors or strings? Or do you mean either scenario? There’s no one-shot solution to cover all of those scenarios based on the number of factors that could go into a sort.

It’s funny how finally giving in and asking for help led me to figuring it out finally! I really wish there was at least a built in sorting command for an array of things like floats, or ints. I understand an array of actors (like I used) might be crazy to build a “one size fits all” kind of function.

Attached is how I sorted an array of actors by their x-coordinates. I wish I could explain it clearly, but all I did was copy some sorting algorithm on Wikipedia and renaming the variables they used, then hammered at it a bit to make it work on actors instead of numbers. You could tweak it to evaluate any property of the actors instead of X.

Lovely, thanks a lot for posting this. It does seem this causes an error however during getting the index of Sorted Target Locs in the bottom left, piped into the while condition. To fix this just add a Clamp (Int) with min set to 0 after the Target Index Stepper - 1 call. As far as I can see it still works that way.

Works great for me either way, just thought I’d mention it.

There’s no one-shot solution to cover
all of those scenarios based on the
number of factors that could go into a
sort.

This is how I do it in Action Script:

var HostServer:Object = {Server:"Test", IP:"192.168.1.1", Mode:"Deathmatch", Map:"MyMap", Players:9, MaxPlayers:32, Ping:55, Locked:false, Password:"123"};

ServerList.push(HostServer);

ServerList.sortOn(["Server"]);

ServerList.sortOn(["IP"],Array.NUMERIC);

ServerList.sortOn(["Players"],Array.NUMERIC | Array.DESCENDING);

I can’t see any reason why we wouldn’t be able to sort arrays and be able to determine the sort type. There’s only a small handful of types available anyway. If it’s a string, it’s alphabetical. If it’s a numerical value, there’s two options (descending or ascending) and that’s almost all that’s ever needed. A simple dropdown in the blueprint node would only need 4 entries. (name of the struct object, alphabetical, ascending and descending).

I’m in blueprints trying to create a struct of objects and their distances from the player. I simply want to sort the array of those structs by the distances to quickly and easily put the closest one at index 0. Taco’s method seems absurdly convoluted in comparison to what I can do in Flash.

The low-entry extended standard library has a sort array by comparator function, which is what you’d need here.

96652-lesort.png

1 Like

I might give you a library with a lot of array sorting things, but I can’t post it right now. Maybe this afternoon or Tuesday.

EDIT: Here is the start of my extended Array library, it nowhere near finished but you should be able to get a few ideas from it.

Thanks Taco. I get the feeling this is a frequently visited thread. Here’s my take on your BP, for sorting an array of actors by horizontal distance to the Player. Cheers for pointing me in the right direction.

Thanks Benni!! Your code helped me so much. Been stuck for more than 4 hours, and with this code i solved it in 10 min haha. Thanks thanks thanks!!!

Except item being stored in an inventory usually won’t be actors, so the Filter function wouldn’t accept an array of Inventory items

Hate to bump something this old but thank you!!! I was getting the -1 index error and it was driving me nuts figuring out where it was. I had the right idea of what I had to do, I just didn’t know where.

Not everyone wants the additional complexity of thunking into Java … just for something that’s been built into STL (native C++, multi-platform)for 20+ years. I’m honestly shocked a simple sort isn’t built into the basic blueprint library.

But, great answer … within the constraints :slight_smile:

The LE library doesn’t use Java. The server counterpart does. But you absolutely don’t need to use the server to use any of the LE standard library extensions such as sorting.

That being said, you’re right. It is quite shocking that such basic things are not included in the standard blueprint library.

Modified this to sort my server browser sessions by ping/latency. Thanks heaps!