# How can I sort items in array by properties?

**URL:** <https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [April 20, 2014, 9:23pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159 "2014-04-20T21:23:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_f0c9cb95](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_f0c9cb95](https://forums.unrealengine.com/u/anonymous_user_f0c9cb95)\
**Post date:** [April 20, 2014, 9:23pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/1 "2014-04-20T21:23:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Althaen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Althaen](https://forums.unrealengine.com/u/Althaen)\
**Post date:** [April 20, 2014, 10:05pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/2 "2014-04-20T22:05:57Z")

</div>

Use a ForEachLoop node.

---

<div class="post-metadata">

**Author:** ![Zeustiak](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeustiak](https://forums.unrealengine.com/u/Zeustiak)\
**Post date:** [April 21, 2014, 2:09am UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/3 "2014-04-21T02:09:26Z")

</div>

Array Doc:  
[https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Arrays/ArrayNodes/index.html](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.

---

<div class="post-metadata">

**Author:** ![Slavq](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Slavq](https://forums.unrealengine.com/u/Slavq)\
**Post date:** [April 22, 2014, 3:01pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/4 "2014-04-22T15:01:14Z")

</div>

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 🙂 I will be grateful for some advice or example of array sorting in Blueprint.

---

<div class="post-metadata">

**Author:** ![TomShannon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tomshannon/32/1145428_2.png) [@TomShannon](https://forums.unrealengine.com/u/TomShannon)\
**Post date:** [May 12, 2014, 5:15pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/5 "2014-05-12T17:15:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zeustiak](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeustiak](https://forums.unrealengine.com/u/Zeustiak)\
**Post date:** [May 13, 2014, 9:59am UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/6 "2014-05-13T09:59:35Z")

</div>

[https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Arrays/ArrayNodes/index.html](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.

---

<div class="post-metadata">

**Author:** ![\_1.99\_Taco\_Combo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@\_1.99\_Taco\_Combo](https://forums.unrealengine.com/u/_1.99_Taco_Combo)\
**Post date:** [June 25, 2014, 5:53am UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/7 "2014-06-25T05:53:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve\_Allison](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Steve\_Allison](https://forums.unrealengine.com/u/Steve_Allison)\
**Post date:** [June 25, 2014, 6:36pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/8 "2014-06-25T18:36:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![\_1.99\_Taco\_Combo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@\_1.99\_Taco\_Combo](https://forums.unrealengine.com/u/_1.99_Taco_Combo)\
**Post date:** [June 26, 2014, 5:21am UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/9 "2014-06-26T05:21:38Z")

</div>

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.

 ![9142-sort_actors_byx.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/5/4/2548eba24300a1dcbf8b9094addc296aeac18084.jpeg)

---

<div class="post-metadata">

**Author:** ![RumbleMonk](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RumbleMonk](https://forums.unrealengine.com/u/RumbleMonk)\
**Post date:** [August 3, 2014, 4:12pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/10 "2014-08-03T16:12:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hitpawz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hitpawz](https://forums.unrealengine.com/u/Hitpawz)\
**Post date:** [November 19, 2014, 3:01am UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/11 "2014-11-19T03:01:31Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_fbe5155f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_fbe5155f](https://forums.unrealengine.com/u/anonymous_user_fbe5155f)\
**Post date:** [June 30, 2016, 1:05pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/12 "2016-06-30T13:05:20Z")

</div>

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

> **[LE Extended Standard Library in Code Plugins - UE Marketplace](https://www.unrealengine.com/marketplace/en-US/product/low-entry-extended-standard-library)**
>
> This plugin provides you with blueprints that add general functionality and improvements.

![96652-lesort.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/e/7/9e7488e17a5b3d15b4d59d8ac6ee22dd4bccf63d.png)

 ![259790-howto.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/6/0/b608653098830441f528d142b7f7da49842084b9.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_210522a1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_210522a1](https://forums.unrealengine.com/u/anonymous_user_210522a1)\
**Post date:** [July 1, 2016, 6:52am UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/13 "2016-07-01T06:52:21Z")

</div>

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.

> **[Google Drive: Sign-in](https://accounts.google.com/v3/signin/identifier?dsh=S1762420766%3A1682417728028389&continue=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D0B-_sl3OdyWWlX19kZ0ZNVUtkODA&followup=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D0B-_sl3OdyWWlX19kZ0ZNVUtkODA&ifkv=AQMjQ7TliI25dIjiingKeTLt7eTgf4n0e_ZjHrMKjn9l6S4vwnT7KAl4b2vEzK1kbIUzz71oNOPg&osid=1&passive=1209600&service=wise&flowName=GlifWebSignIn&flowEntry=ServiceLogin)**
>
> Access Google Drive with a Google account (for personal use) or Google Workspace account (for business use).

-

---

<div class="post-metadata">

**Author:** ![Benni.Lodge](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Benni.Lodge](https://forums.unrealengine.com/u/Benni.Lodge)\
**Post date:** [March 6, 2017, 12:14pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/14 "2017-03-06T12:14:54Z")

</div>

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.

 ![128841-sortingalgorithm_1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/7/1/871a54f1d24e95a99b989f5d4d4cddd24b5d3f17.png)

---

<div class="post-metadata">

**Author:** ![Vi-So\_Construct](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/vi-so_construct/32/608276_2.png) [@Vi-So\_Construct](https://forums.unrealengine.com/u/Vi-So_Construct)\
**Post date:** [November 24, 2017, 2:59pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/15 "2017-11-24T14:59:27Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_705e34d1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_705e34d1](https://forums.unrealengine.com/u/anonymous_user_705e34d1)\
**Post date:** [October 22, 2018, 7:53pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/16 "2018-10-22T19:53:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nostrildumbass](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nostrildumbass/32/261258_2.png) [@Nostrildumbass](https://forums.unrealengine.com/u/Nostrildumbass)\
**Post date:** [March 4, 2019, 2:57am UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/17 "2019-03-04T02:57:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JGwinner](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JGwinner](https://forums.unrealengine.com/u/JGwinner)\
**Post date:** [August 8, 2019, 1:17am UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/18 "2019-08-08T01:17:10Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Hans\_im\_Wahn](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hans\_im\_Wahn](https://forums.unrealengine.com/u/Hans_im_Wahn)\
**Post date:** [July 9, 2020, 9:40pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/19 "2020-07-09T21:40:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mbwilding](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mbwilding](https://forums.unrealengine.com/u/mbwilding)\
**Post date:** [March 28, 2021, 6:34pm UTC](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159/20 "2021-03-28T18:34:06Z")

</div>

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

[Next page](https://forums.unrealengine.com/t/how-can-i-sort-items-in-array-by-properties/281159.md?page=2)
