Results 1 to 7 of 7
  1. #1
    The Sacrifice
    Join Date
    Apr 2012
    Posts
    133

    Default Using Array function Sort() for struct elements

    i have an array with struct elements and want to sort them in a simple way.
    those elements should be sort by compare struct.value which is an int value.

    Unrealwiki says to Array.Sort(comparefunction):
    The comparefunction is the name of a function that takes two parameters of the array's element type and returns a value of type int.
    i dont if it works for any type

    my question is, if its possible to write such a comparefunction:

    function int comparefunction(struct a, struct b)
    {
    return a.value-b.value;
    }

  2. #2

    Default

    I see no reason why you can't. Of course, the only real way to know is to write the function and test it out
    Need an online game service for your project? Check out Sagittarius! For more info you can visit the Sagittarius Wiki or the project page on my blog.

    My Blog: WillyG Productions - Your Premiere UDK Resource
    My YouTube Channel: WillyG Productions
    My Facebook Page: WillyG Productions
    My GitHub: willyg302

  3. #3

    Default

    Sorting is super confusing and poorly documented. Maybe someone can help me. If I get a good response, I'll think about adding the changes to BeyondUnreal and submit a change request to Epic's documentation as well.

    The UDN says:

    "Sort(SortDelegate): uses SortDelegate to sort the contents of the array in-place. SortDelegate should have signature matching the following:
    delegate int ExampleSort(ArrayType A, ArrayType B)
    { return A < B ? -1 : 0; } // a negative return value indicates the items should be swapped"

    First, I notice everyone keeps making functions, but shouldn't it be a delegate block and not a function block as the UDN documentation suggest, or is the documentation mistaken?
    Second, There is no explanation of what A and B are precisely or where they come from, or any decent example on the UDN or the BeyondUnreal Wiki.
    Does running a sort iterate automatically through the entire array as needed, or do I need to write extra code loops for it to run through the whole array?

    So far, this is what I have:

    // Begin Sort Delegates //

    delegate int OpenListCostSort( Node A, Node B)
    {
    return A.TotalCost > B.TotalCost ? -1 : 0;
    }

    // End Sort Delegates //


    // Begin Function //

    local array<Node> OpenList;

    OpenList.Sort(OpenListCostSort);

    // End Function //



    Will this properly sort my entire array with Ascending TotalCost, or do I need to write some 'for' loops? Is my sort even being called correctly, or do I need to give it A and B like a regular function call?

    The Dynamic array in question will need to be able to sort tens of thousands or even hundreds of thousands of elements and often, so I hope this won't be too costly, but it probably will be. Sometimes I simply can't stand how under-documented the UDK is...

  4. #4

    Default

    Dude, UDK is superbly documented, compared to how it was back in the day up to UT2004. Also, what's so hard to understand about that part? The comparison function returns an int value, whose sign tells, whether elements are in the correct order. Array.Sort() is a native function that calls the comparison function whenever two elements need to be compared for correct order in the array. What algorithm Sort() actually uses is irrelevant (though I'd expect it to be some flavor of Quicksort), you only need to care about the comparison function.

    A "delegate block" really is a delegate variable and an anonymous function acting as default value for the delegate. The only delegate involved is the parameter of the Sort() function, to which you pass a matching function reference as usual.
    Wormbo's UT/UT2004/UT3 mods | PlanetJailbreak | Unreal Wiki | Liandri Archives

    <@Mych|Lockdown> ...and the award for the most creative spelling of "Jailbreak" goes to ... "Gandis Jealbrake Server"
    Note that your questions via PMs will be ignored if they actually belong in the forum.

  5. #5

    Default

    Quote Originally Posted by Wormbo View Post
    Dude, UDK is superbly documented, compared to how it was back in the day up to UT2004. Also, what's so hard to understand about that part? The comparison function returns an int value, whose sign tells, whether elements are in the correct order. Array.Sort() is a native function that calls the comparison function whenever two elements need to be compared for correct order in the array. What algorithm Sort() actually uses is irrelevant (though I'd expect it to be some flavor of Quicksort), you only need to care about the comparison function.

    A "delegate block" really is a delegate variable and an anonymous function acting as default value for the delegate. The only delegate involved is the parameter of the Sort() function, to which you pass a matching function reference as usual.
    First, thanks for the response! What you add to the documentation expands my understanding greatly, if I'm understanding you. Things like "you only need to care about the comparison function" is the kind of thing I was hoping for from the documentation.

    Second, I agree the documentation must be much better than in earlier days, but I still find things that are either not documented at all, or are just plain wrong. My perspective is different from yours, and I'm not saying my perspective is the only one, because it's not. I'm just voicing my own frustrations, which are normally jumped on by others with a lot more knowledge, so I should know better by now. Maybe I'm just a stickler for knowing why things happen under the hood, but I can tell you from my standpoint of being a beginner, the documentation is still inadequate. No doubt you are far more experienced than I, so you can pull from your knowledge to fill in the blanks; I sadly do not yet have that knowledge, so I often find myself scratching my head when I find something woefully under-explained. I try hard not to get angry at the documentation, but I just can't help it sometimes.

    Lastly, from what I can gather from your response, I don't have to do anything else, as my code is at least very close, and my array will come out fully sorted? If that's wrong, let me know. I'm truly grateful for the response.

  6. #6
    Boomshot
    Join Date
    Aug 2011
    Posts
    2,920

    Default

    Quote Originally Posted by krillin6 View Post
    Lastly, from what I can gather from your response, I don't have to do anything else, as my code is at least very close, and my array will come out fully sorted?
    Test, test, test! Sanity checks and test results are underrated tools.

  7. #7

    Default

    krillin6, you need to put more efforts into your research. Just the very construct of "array.Sort(comparefunction)" already tells you that you only need an "array" and a "compare function" to satisfy the requirements of the "sort" function.
    Wormbo's UT/UT2004/UT3 mods | PlanetJailbreak | Unreal Wiki | Liandri Archives

    <@Mych|Lockdown> ...and the award for the most creative spelling of "Jailbreak" goes to ... "Gandis Jealbrake Server"
    Note that your questions via PMs will be ignored if they actually belong in the forum.


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright ©2009-2011 Epic Games, Inc. All Rights Reserved.
Digital Point modules: Sphinx-based search vBulletin skin by CompletevB.com.