# Currently impossible to implement polymorphic quicksort in Verse?

**URL:** <https://forums.unrealengine.com/t/currently-impossible-to-implement-polymorphic-quicksort-in-verse/1147764>\
**Category:** Verse\
**Tags:** fortnite-creative, unreal-editor-for-fortnite, question, fortnite\
**Created:** [April 28, 2023, 8:06pm UTC](https://forums.unrealengine.com/t/currently-impossible-to-implement-polymorphic-quicksort-in-verse/1147764 "2023-04-28T20:06:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![meatandmeat](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@meatandmeat](https://forums.unrealengine.com/u/meatandmeat)\
**Post date:** [April 28, 2023, 8:06pm UTC](https://forums.unrealengine.com/t/currently-impossible-to-implement-polymorphic-quicksort-in-verse/1147764/1 "2023-04-28T20:06:56Z")

</div>

I tried implementing Quicksort based on a Wikipedia example:

```plaintext
(Array:[]t where t:type).Partition(Low:int, High:int):int=
    if(Pivot := Array[Floor[(High*1.0-Low*1.0)/2.0]+Low]):
        var Left : int = Low-1
        var Right : int = High+1
        loop:
            loop:
                set Left += 1
                if(Array[Left] >= Pivot). break
            loop:
                set Right -= 1
                if(Array[Right] <= Pivot). break
            if(Left>=Right). return Right
            Tmp := Left
            set Left = Right
            set Right = Left
    return 0

(Array:[]t where t:type).Sort(Low:int, High:int):[]t=
    if(Low >= 0 and High >= 0 and Low < High):
        P := Array.Partition(Low, High)
        Array.Sort(Array,Low,P)
        Array.Sort(Array,P+1,High)
    return Array

```

but it isn’t compiling because it doesn’t know which overload of +, -, \> and \< to use.

I assume this should be possible in the future as it doesn’t seem that difficult for the compiler to figure out, but it currently seems impossible?

Or does anyone know if there’s a way to work around this issue?

Edit: I realize I really screwed up the algorithm and the code is completely broken, I really should have made a non-polymorphic version and tested it before posting 🫤 But anyways the main issue is still whether there’s a way to get Verse to use the correct overloads currently

Edit: Here’s a working non-polymorphic version:

```plaintext
int_array_ref := class<unique>:
    var Data : []int = array{}
        
(Array:int_array_ref).Partition(Low:int, High:int):int=
    if(Pivot := Array.Data[Floor[(High*1.0-Low*1.0)/2.0]+Low]):
        var Left : int = Low-1
        var Right : int = High+1
        loop:
            loop:
                set Left += 1
                if(Array.Data[Left]>=Pivot). break
            loop:
                set Right -= 1
                if(Array.Data[Right]<=Pivot). break
            if(Left >= Right). return Right
            if(Tmp := Array.Data[Left]):
                if(set Array.Data[Left] = Array.Data[Right]){}
                if(set Array.Data[Right] = Tmp){}
    return 0

(Array:int_array_ref).Sort(Low:int, High:int):void=
    if(Low >= 0 and High >= 0 and Low < High):
        P := Array.Partition(Low, High)
        Array.Sort(Low,P)
        Array.Sort(P+1,High)

```

---

<div class="post-metadata">

**Author:** ![Tim\_Sweeney](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tim\_Sweeney](https://forums.unrealengine.com/u/Tim_Sweeney)\
**Post date:** [April 30, 2023, 4:33pm UTC](https://forums.unrealengine.com/t/currently-impossible-to-implement-polymorphic-quicksort-in-verse/1147764/2 "2023-04-30T16:33:00Z")

</div>

You wrote that beautifully, but we haven’t built the features needed to support this yet. We need something resembling C# type constraints ( [where (generic type constraint) - C# Reference | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint)), Rust traits, or Haskell typeclasses in order to express the constraint that “t is required to be some type supporting ordered comparison”.

---

<div class="post-metadata">

**Author:** ![gontux](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/gontux/32/642085_2.png) [@gontux](https://forums.unrealengine.com/u/gontux)\
**Post date:** [April 30, 2023, 9:43pm UTC](https://forums.unrealengine.com/t/currently-impossible-to-implement-polymorphic-quicksort-in-verse/1147764/3 "2023-04-30T21:43:02Z")

</div>

Here is a quick sort implementation I made with the help of ChatGPT and how other languages implement it, adapted to Verse.  
It’s slightly different from yours in that it uses a custom comparison function to sort the data.

```auto
(Input:[]t where t:type).QuickSort<public>(CompareFunction(Element1:t, Element2:t):int):[]t = 

    if (Input.Length <= 1):
        return Input

    var Less : []t = array {}
    var Equal : []t = array {}
    var Greater : []t = array {}

    if (Pivot := Input[Int[Input.Length * 0.5]]):
        for (Element : Input):
            Result := CompareFunction(Element, Pivot)
            ArrayElement := array { Element }
            if (Result < 0):
                set Less += ArrayElement
            else if (Result > 0):
                set Greater += ArrayElement
            else:
                set Equal += ArrayElement

    Less.QuickSort(CompareFunction) + Equal + Greater.QuickSort(CompareFunction)

```

Usage: Having the following array of integers…

```auto
IntArray : []int = array { 3, 4, 1, 0, -6, 10, 1 }

```

you define a comparison function for integers:

```auto
# The comparison function should return:
# -1 if Element1 should go before Element2
# 0 if Element1 and Element2 are equal
# 1 if Element1 should go after Element2
SortFunction(Element1: int, Element2: int): int =
    if (Element1 > Element2):
        1
    else if (Element1 < Element2):
        -1
    else:
        0

```

Then you call the QuickSort method passing the custom comparision function:

```auto
SortedArray := IntArray.Sort(SortFunction)

```

This way you can also sort any kind of complex data, not just integers, by providing an appropriate comparison function.

---

<div class="post-metadata">

**Author:** ![Tim\_Sweeney](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tim\_Sweeney](https://forums.unrealengine.com/u/Tim_Sweeney)\
**Post date:** [May 20, 2023, 10:34pm UTC](https://forums.unrealengine.com/t/currently-impossible-to-implement-polymorphic-quicksort-in-verse/1147764/4 "2023-05-20T22:34:30Z")

</div>

Beautiful! And your approach works for writing a sort function to sort ascending or descending, sort structures based on field comparisons in some order, and so on.

---

<div class="post-metadata">

**Author:** ![Nman\_Pkr](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nman_pkr/32/1194697_2.png) [@Nman\_Pkr](https://forums.unrealengine.com/u/Nman_Pkr)\
**Post date:** [September 24, 2025, 1:06am UTC](https://forums.unrealengine.com/t/currently-impossible-to-implement-polymorphic-quicksort-in-verse/1147764/5 "2025-09-24T01:06:38Z")

</div>

I was able to get this to sort 200 elements a frame, much better then the insertion sort I was doing, thank you.

---

<div class="post-metadata">

**Author:** ![SonnyGT102](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SonnyGT102](https://forums.unrealengine.com/u/SonnyGT102)\
**Post date:** [October 5, 2026, 8:29pm UTC](https://forums.unrealengine.com/t/currently-impossible-to-implement-polymorphic-quicksort-in-verse/1147764/6 "2026-10-05T20:29:28Z")

</div>

@gontux Hola! Disculpá que te contacte por acá. Estoy intentando comunicarme con vos por un proyecto antiguo de Rocket League para CS 1.6 que te había comprado alrededor de 2020 y necesito comprar otra vez. Mi cuenta no me permite enviarte mensaje privado. ¿Podrías mandarme un PM cuando puedas? Te mandé solicitud en discord también Gracias!
