WTF? Sort a list by an object of the list.



  • Found this today in the Business Framework. 

    I don't care if it's something you'd expect an object to do, it's still a WTF.  My first instinct would be to make it static, but that won't work.

     

    Declarations (this is C#)

    public string ThingCode {get, set}
        Member of Company.Business.IThing

    public abstract new Company.Business.IThing[] Sort ( Company.Business.IThing[] thingArray , params string[] thingCodes )
        Member of Company.Business.IThing

    (IThing is an interface for Thing; I assume that the thing object determines how the implementation is handled, but that's even worse of a WTF, as different IThings could sort in drastically different ways.)

    Note: in common usage, Things will generally have unique ThingCodes.

     

    So to use this lovely mess, we do something like this:

    IThing[] thingList = this.GetThingList(); //made up method

    IThing[] sortedthingList = thingList[0].Sort(thingList, "A","B","C","D");

    So no matter how many objects thingList contains, you will only end up with a maximum of 4 things if you use this, noting the common usage caveat.  I have no clue what happens with dupe thing codes (not something we normally do).

    This is a nice way to make a hard-coded include filter as well, which is the part that caught me by surprise--this is included in some important areas by default...

     

    And yes, it could be worse:

    IThing[] thingList = this.GetThingList()[0].Sort(this.GetThingList(), "A","B","C","D");

    Noting of course that you hope and pray GetThingList gives you at least one IThing the first time you call it in that line. :)

     

    I personally just made a IComparer, used Array.Sort() and got on with my life.



  • Uh, what?



  • That is incredible.  Someone clearly doesn't understand .NET collections. Your solution is far better.



  • joe_bruin: In .NET you usually just implement ICompare and just need an instance of the array, and call sort.

    The poor bastard aforementioned (wow that's a cool word -- I could be a professor at WTFU!) aparently implemented it as an instance method that requires you to not invoke the sort from the instance, and only sorts up to four objects apparently... lame.



  • [quote user="BrainSlugs83"]

    joe_bruin: In .NET you usually just implement ICompare and just need an instance of the array, and call sort.

    The poor bastard aforementioned (wow that's a cool word -- I could be a professor at WTFU!) aparently implemented it as an instance method that requires you to not invoke the sort from the instance, and only sorts up to four objects apparently... lame.

    [/quote]

    Well it can sort any number of objects (it's a params string) but you need to enumerate all of them in the method call somehow.  Anything NOT in that hardcoded order will not show up in the returned array.  You of course can do tricks with string arrays but...still.

    4 was from my example, sorry I wasn't clearer on that point. ("A","B","C","D","E" can return up to 5 objects, if those thing codes are in the list.)
     

    Other WTFs stand. :)



  • I'm just getting my head around .NET - coming from a Perl background I'm really hating all the strong typing, lack of implicit casting etc but overall it's quite nice.

    I don't see why it's limited to 4 ThingCodes - it looks more like he wanted to specify a list of ThingCodes in a set order and return an array of Things in that order - maybe the user could arbitrarily sort Things on screen?

    Even so, definitely Things that make you go hmmm...



  • [quote user="versatilia"]

    I'm just getting my head around .NET - coming from a Perl background I'm really hating all the strong typing, lack of implicit casting etc but overall it's quite nice.

    [/quote]

    Funny, those are the exact things that I love about it. :)



  • [quote user="versatilia"]

    I'm just getting my head around .NET - coming from a Perl background I'm really hating all the strong typing, lack of implicit casting etc but overall it's quite nice.

    I don't see why it's limited to 4 ThingCodes - it looks more like he wanted to specify a list of ThingCodes in a set order and return an array of Things in that order - maybe the user could arbitrarily sort Things on screen?

    Even so, definitely Things that make you go hmmm...

    [/quote]

    It's not.  The function takes a params list--from what I understand, you can pass anything as that list (arrays, separate objects, combinations) and it'll be treated as one string[] as far as the sort function is concerned.

    My example, since it was only passed 4 ThingCodes, will only return the objects with one of those 4 thing codes.  (In common usage here ThingCodes are unique, so I get a max of 4 Things)


Log in to reply