I obtained the following sorting code sample through Googling:
Sort(Items : []t, IsAscending : logic, Comparer: type{_(:t, :t)<computes> : int} where t : type)<computes> : []t =
if (Items.Length > 1, Pivot := Items[Floor(Items.Length/2)]):
Left := for(Item : Items, Comparer(Item, Pivot) < 0) do Item
Middle := for(Item : Items, Comparer(Item, Pivot) = 0) do Item
Right := for(Item : Items, Comparer(Item, Pivot) > 0) do Item
if(IsAscending?):
Sort(Left, IsAscending, Comparer) + Middle + Sort(Right, IsAscending, Comparer)
else:
Sort(Right, IsAscending, Comparer) + Middle + Sort(Left, IsAscending, Comparer)
else:
Items
Here, I added the following methods to simplify the sorting code for the main types:
#Sort Methods By Type
CompareInts<internal>(A : int, B : int)<computes>: int =
if(A < B) then -1
else if(A > B) then 1
else 0
CompareFloats<internal>(A : float, B : float)<computes>: int =
if(A < B) then -1
else if(A > B) then 1
else 0
(Items:[]int).SortInts(IsAscending : logic):[]int =
Sort(Items,IsAscending,CompareInts)
(Items:[]float).SortFloats(IsAscending : logic):[]float =
Sort(Items,IsAscending,CompareFloats)
If I name both SortInts and SortFloat as Sort, it outputs a compiler error saying,
The function operator’.Sort’(:float,:tuple(),:logic) is ambiguous with this definition: function operator’.Sort’(:int,:tuple(),:logic) (3532)
It was inconvenient because I couldn’t handle functions with the same name even if the parameters were different.
Therefore, I want to implement a single function Sort(:t,:logic) that automatically handles according to the type of Items using parametric.
Is there a function like TypeOf() or GetType() that can check the type of Items, or is there another way?