Download

Help with sorting a struct by alphabetic order

I’m making a spelling game and I need help with sorting an array of structs of words in alphabetic order. (Each Struct has a string member called word and I need to sort by it)
I have a Blueprint FunctionLibrary C++ That needs to be exposed to blueprints. I think I got that part covered not sure though first time trying to use it. Very new to C++
For the Sort function I got it from the unreal Docs I Just removed the length part, because I’m trying to not sort by length.
I get errors

 FunctionLibrary.cpp(10): [C2059] syntax error: '.'
  FunctionLibrary.cpp(10): [C2143] syntax error: missing ';' before '{'
  FunctionLibrary.cpp(11): [C2065] 'A': undeclared identifier
  FunctionLibrary.cpp(11): [C2065] 'B': undeclared identifier
  FunctionLibrary.cpp(12): [C2059] syntax error: ')'

FunctionLibrary.cpp

#include "FunctionLibrary.h"
#include "UStructures.h"
#include "Containers/Array.h" // This is greyed out for some reason... Is this how to add an array header I got it from the docs.

void UFunctionLibrary::sortStruct()
{
	struct FProfileStruct.WordArray.Sort([](const FString& A, const FString& B) {
	return A < B;
	});
}

UStructures.h

#pragma once

#include "CoreMinimal.h"

/**
 * 
 */
#include "UStructures.generated.h"

struct FWordStruct;
USTRUCT(BlueprintType)
struct FWordStruct 
{
	GENERATED_BODY()
	UPROPERTY(BlueprintReadWrite)
	FString Word;
	UPROPERTY(BlueprintReadWrite)
	float DisplayTime = 4.5;
	UPROPERTY(BlueprintReadWrite)
	int32 Stars;
	UPROPERTY(BlueprintReadWrite)
	int32 Tested;
	UPROPERTY(BlueprintReadWrite)
	int32 Correct;
	UPROPERTY(BlueprintReadWrite)
	int32 Incorrect;
	UPROPERTY(BlueprintReadWrite)
	int32 Chain;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar0 = true;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar1 = true;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar2 = true;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar3 = true;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar4 = true;
};

USTRUCT(BlueprintType)
struct FProfileStruct 
{
	GENERATED_BODY()
	UPROPERTY(BlueprintReadWrite)
	TArray<FWordStruct> WordArray;
	UPROPERTY(BlueprintReadWrite)
	TArray<FWordStruct> TempWordArray;
	UPROPERTY(BlueprintReadWrite)
	FString Player;
	UPROPERTY(BlueprintReadWrite)
	int32 StarCount;
	UPROPERTY(BlueprintReadWrite)
	int32 WordCount;
	UPROPERTY(BlueprintReadWrite)
	int32 TestedWords;
	UPROPERTY(BlueprintReadWrite)
	bool ShowTutorial = true;
	UPROPERTY(BlueprintReadWrite)
	bool ShowCountdown = true;

};

So I thought it was Scope so I made a member function in UStructures.cpp like so…

#pragma once

#include "CoreMinimal.h"

/**
 * 
 */
#include "UStructures.generated.h" 

struct FWordStruct;
USTRUCT(BlueprintType)
struct FWordStruct 
{
	GENERATED_BODY()
	UPROPERTY(BlueprintReadWrite)
	FString Word;
	UPROPERTY(BlueprintReadWrite)
	float DisplayTime = 4.5;
	UPROPERTY(BlueprintReadWrite)
	int32 Stars;
	UPROPERTY(BlueprintReadWrite)
	int32 Tested;
	UPROPERTY(BlueprintReadWrite)
	int32 Correct;
	UPROPERTY(BlueprintReadWrite)
	int32 Incorrect;
	UPROPERTY(BlueprintReadWrite)
	int32 Chain;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar0 = true;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar1 = true;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar2 = true;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar3 = true;
	UPROPERTY(BlueprintReadWrite)
	bool NewStar4 = true;
};

USTRUCT(BlueprintType)
struct FProfileStruct 
{
	GENERATED_BODY()
	UPROPERTY(BlueprintReadWrite)
	TArray<FWordStruct> WordArray;
	UPROPERTY(BlueprintReadWrite)
	TArray<FWordStruct> TempWordArray;
	UPROPERTY(BlueprintReadWrite)
	FString Player;
	UPROPERTY(BlueprintReadWrite)
	int32 StarCount;
	UPROPERTY(BlueprintReadWrite)
	int32 WordCount;
	UPROPERTY(BlueprintReadWrite)
	int32 TestedWords;
	UPROPERTY(BlueprintReadWrite)
	bool ShowTutorial = true;
	UPROPERTY(BlueprintReadWrite)
	bool ShowCountdown = true;

	void SortStructArray()
	{
		WordArray.Sort([](const FString& A, const FString& B) {
return A < B;
});
	}
};

Which works at first glance until I try to call the function.

#include "FunctionLibrary.h"
#include "UStructures.h"
#include "Containers/Array.h"

void UFunctionLibrary::sortStruct()
{
	struct FProfileStruct.SortStructArray();
}
	
	
	

How do I use a struct from another file and how to sort a struct by alphabetic order?

You’ve Declared your struct. You don’t have a instance of it anywhere however, hence why it’s complaining.

FProfileStruct.SortStructArray(); // Invalid, FProfileStruct isn't a namespace or anything. It's just the layout of a structure.

FProfileStruct MyStruct; // Instantiate a local instance of FProfileStruct
MyStruct.SortStructArray(); // Valid. Sort this instance of FProfileStruct.

Now, that’s the syntax portion of your question. The larger question is I assume you only want one of these structures at runtime and want to be able to get it and sort it as needed. That would require some more code and placing the structure in a SubSystem, GameState, or even the Player depending on your requirements.

Basically you need to ask yourself these questions:

  1. Who “owns” this information? The game? Each player?
  2. Where should I put this variable so it is easily reachable to the systems I want to access it? If this is a server only value, I’d likely put it on the GameState with various “HasAuthority” checks. If it’s each Player, then I would just toss it there. If it’s an Offline game and I know I need one per level or through out the lifetime of the game, I’d use a subsystem.

Hope that helps.

Thank you for your reply I have a question if I use a SubSystem will it work?
because I get compile errors about the SortStructArray Function member when I try it without it.
I have a blueprint Game instance isn’t that the same thing?

Anything will work. It’s just a simple struct. There’s no reason why a WorldSubsystem or what not wouldn’t work. You can use the GameInstance but that exists for the lifetime of the application I believe. If that works for you, then go for it.

Just remember:

// In C/C++ declaring an object works like such
// <Type> <VariableName>
int MyInt;
float MyFloat;
FProfileStruct MyStruct; // FProfileStruct is the TYPE of object, MyStruct is the actual object/variable.

That makes no sese when unreal uses int32 Fstring Float ect

FString is a struct.

FString SomeString(TEXT("ABCD")); // Simple variable.
FString MyFormattedString = FString::Format(TEXT("%s"), *SomeString); // A STATIC function within a struct.

// Static functions can be called from anywhere, but they aren't actually instances.
// They do some operation and return a result on other objects. They have no memory themselves.