{"id":8433,"date":"2023-07-10T12:36:09","date_gmt":"2023-07-10T10:36:09","guid":{"rendered":"https:\/\/store.algosyntax.com\/?post_type=asx-lms-tutorial-cpt&#038;p=8433"},"modified":"2026-03-09T01:22:16","modified_gmt":"2026-03-08T23:22:16","slug":"sorting-in-unreal-engine-5-a-tarray-and-tmap-primer","status":"publish","type":"asx-lms-tutorial-cpt","link":"https:\/\/store.algosyntax.com\/tutorials\/unreal-engine\/sorting-in-unreal-engine-5-a-tarray-and-tmap-primer\/","title":{"rendered":"Sorting in Unreal Engine 5: A TArray and TMap Primer"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"8433\" class=\"elementor elementor-8433\" data-elementor-post-type=\"asx-lms-tutorial-cpt\">\n\t\t\t\t<div class=\"elementor-element elementor-element-a613f5c e-flex e-con-boxed e-con e-parent\" data-id=\"a613f5c\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-20cf4d5 elementor-widget elementor-widget-text-editor\" data-id=\"20cf4d5\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>Sorting is an essential operation in computer programming that helps us organize data in a particular order, making it easier to search, analyze, and visualize. In Unreal Engine 5 (UE5), data sorting is predominantly used with the dynamic array data structure TArray. However, the concepts of sorting can be applied beyond TArray as well. In this article, we will cover the basics of sorting in UE5, focusing primarily on TArray and touching on the applications of sorting with other data structures.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c8cfb6c elementor-widget elementor-widget-heading\" data-id=\"c8cfb6c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Sorting TArray: The Basics\n<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fb66c16 elementor-widget elementor-widget-text-editor\" data-id=\"fb66c16\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>In UE5, the TArray data structure offers a built-in method for sorting its elements. The default sorting operation organizes the TArray in ascending order, but you can customize this behavior by providing a sorting function or lambda. Here&#8217;s a simple example:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5aff86f elementor-widget elementor-widget-code-highlight\" data-id=\"5aff86f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>TArray<int32> MyArray = {3, 1, 4, 1, 5, 9};\r\nMyArray.Sort();\r\n\/\/ MyArray is now {1, 1, 3, 4, 5, 9}\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-783e654 elementor-widget elementor-widget-heading\" data-id=\"783e654\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Custom Sorting with Lambdas\n<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ab55f20 elementor-widget elementor-widget-text-editor\" data-id=\"ab55f20\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>UE5 allows developers to define a custom sorting function via a lambda. This feature is helpful when sorting complex objects. Let&#8217;s consider an example where we have a TArray of players, and we want to sort them based on their scores:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9550794 elementor-widget elementor-widget-code-highlight\" data-id=\"9550794\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>TArray<FPlayer> Players;\r\n\/\/ Assume Players is filled with player objects\r\n\r\n\/\/ Sort players based on score, in descending order\r\nPlayers.Sort([](const FPlayer& A, const FPlayer& B) {\r\n    return A.Score > B.Score;\r\n});\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7978f95 elementor-widget elementor-widget-heading\" data-id=\"7978f95\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Sorting Other Data Structures\n<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3fe78d4 elementor-widget elementor-widget-text-editor\" data-id=\"3fe78d4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>Although TArray is the most commonly sorted data structure, the concepts and importance of sorting apply to other data structures in UE5.<\/p><p>For example, you might have a TMap storing players and their scores, and you might want to create a sorted TArray of player names for displaying a leaderboard. Here&#8217;s how you could do that:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-95d5db3 elementor-widget elementor-widget-code-highlight\" data-id=\"95d5db3\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard \">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-cpp line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-cpp\">\n\t\t\t\t\t<xmp>TMap<FString, int32> PlayerScores;\r\n\/\/ Assume PlayerScores is filled\r\n\r\n\/\/ Create a TArray of player names\r\nTArray<FString> PlayerNames = PlayerScores.GetKeys();\r\n\r\n\/\/ Sort player names based on scores, in descending order\r\nPlayerNames.Sort([&](const FString& A, const FString& B) {\r\n    return PlayerScores[A] > PlayerScores[B];\r\n});\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ca87722 elementor-widget elementor-widget-text-editor\" data-id=\"ca87722\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>In this example, sorting the TArray helps us organize the TMap&#8217;s data for a specific purpose, demonstrating how sorting can be applied beyond TArrays.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0fe7c74 elementor-widget elementor-widget-heading\" data-id=\"0fe7c74\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\">Conclusion<\/h2>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-550eaaa elementor-widget elementor-widget-text-editor\" data-id=\"550eaaa\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>We hope this tutorial helped you learn more about sorting in unreal engine. You may be confused about the concept of &#8220;Predicates&#8221; which are used by various algorithms for TArray, TMap and TSet. You can read more about Predicates here : <a href=\"https:\/\/store.algosyntax.com\/tutorials\/unreal-engine\/ue5-creating-predicates-for-tarray-tmap-and-tsets\/\">Creating Predicates for TArray, TMap and TSet<\/a><br \/><br \/><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-59ecf1dd e-flex e-con-boxed e-con e-parent\" data-id=\"59ecf1dd\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t<div class=\"elementor-element elementor-element-43391a68 e-con-full e-flex e-con e-child\" data-id=\"43391a68\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-29d170f7 elementor-widget elementor-widget-heading\" data-id=\"29d170f7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<p class=\"elementor-heading-title elementor-size-default\">Checkout Our Marketplace<\/p>\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3659aa81 elementor-widget elementor-widget-image\" data-id=\"3659aa81\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<a href=\"https:\/\/www.unrealengine.com\/marketplace\/en-US\/product\/blender-curves-importer\" target=\"_blank\" rel=\"noopener\">\n\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"540\" height=\"540\" src=\"https:\/\/store.algosyntax.com\/wp-content\/uploads\/marketplace\/unreal-engine\/spline-importer.png\" class=\"attachment-full size-full wp-image-6109\" alt=\"Blender To UE5 Spline Importer\" \/>\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-3ef0ad31 e-con-full e-flex e-con e-child\" data-id=\"3ef0ad31\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-56671d25 elementor-widget elementor-widget-heading\" data-id=\"56671d25\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<p class=\"elementor-heading-title elementor-size-default\">Exports Splines From Blender To Unreal Engine in 2 clicks! \n<\/p>\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-2d8f3db7 e-con-full e-flex e-con e-child\" data-id=\"2d8f3db7\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-6a62e317 elementor-widget__width-auto elementor-widget elementor-widget-heading\" data-id=\"6a62e317\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t\t<p class=\"elementor-heading-title elementor-size-default\"><a href=\"https:\/\/www.unrealengine.com\/marketplace\/en-US\/product\/blender-curves-importer\" target=\"_blank\" rel=\"noopener\">$29!<\/a><\/p>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-64c69755 elementor-widget elementor-widget-button\" data-id=\"64c69755\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"button.default\">\n\t\t\t\t\t\t\t\t\t\t<a class=\"elementor-button elementor-button-link elementor-size-sm elementor-animation-grow-rotate\" href=\"https:\/\/www.unrealengine.com\/marketplace\/en-US\/product\/blender-curves-importer\" target=\"_blank\" rel=\"noopener\">\n\t\t\t\t\t\t<span class=\"elementor-button-content-wrapper\">\n\t\t\t\t\t\t\t\t\t<span class=\"elementor-button-text\">Download Blender Spline Importer<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-38aeebd7 elementor-widget elementor-widget-heading\" data-id=\"38aeebd7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-71a4a2b1 elementor-widget elementor-widget-text-editor\" data-id=\"71a4a2b1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p><strong>Blender Spline Importer<\/strong> allows you to import Blender Curves into unreal engine 4\/5 as USplinecomponents! Don&#8217;t miss the sale!<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"featured_media":0,"template":"","asx-lms-tutorial-categories":[45,58],"asx-lms-tutorial-tags":[46],"class_list":["post-8433","asx-lms-tutorial-cpt","type-asx-lms-tutorial-cpt","status-publish","hentry","asx-lms-tutorial-categories-unreal-engine","asx-lms-tutorial-categories-unreal-c-api","asx-lms-tutorial-tags-cpp","entry"],"acf":[],"_links":{"self":[{"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-cpt\/8433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-cpt"}],"about":[{"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/types\/asx-lms-tutorial-cpt"}],"version-history":[{"count":0,"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-cpt\/8433\/revisions"}],"wp:attachment":[{"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/media?parent=8433"}],"wp:term":[{"taxonomy":"asx-lms-tutorial-categories","embeddable":true,"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-categories?post=8433"},{"taxonomy":"asx-lms-tutorial-tags","embeddable":true,"href":"https:\/\/store.algosyntax.com\/asx-rest\/wp\/v2\/asx-lms-tutorial-tags?post=8433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}