Sort TArray of Components by Name Alphabetically Ascending

Hello! I have an array of scene components and I was wondering, how would I alphabetically sort this array (using the name’s of the components in the blueprint) and have them ascending? Thanks! :slight_smile:

You can use TArray.Sort, there are two options:

  • Define a < operator for two const USsceneComponents and use MyArray.Sort() with no arguments.
  • Define a custom named comparison function and call MyArray.Sort(…) with that function as argument.

I recommend the second option because having a < operator for USceneComponent does not have a general intuitive meaning. Here is someone’s working example, scroll down to the answer:

Edit: Oh yeah, as for your comparison function implementation, you can do < between any FStrings A, B to test whether A appears first lexicographically. Not sure if that works for FNames, but you can convert any FName to a string with .ToString(). :slight_smile:

1 Like

Thanks! :slight_smile: