locked
What is the function of the VB Comparison constructor when passed as a argument to SORT method of List Collection RRS feed

  • Question

  • User1909155429 posted

    I am trying to sort my listcollection object using the following code; i dont know the purpose of the comparison expression function object  and what operation it performs as the USERNAME property being passed share identical values?

    All i know is that the sort method will not work without it? and i need to understand the operation if i want to use if with other property values passed in.

    
    
     comparison = New Comparison(Of MembershipUserWrapper)(Function(lhs As MembershipUserWrapper, rhs As MembershipUserWrapper) lhs.UserName.CompareTo(rhs.UserName))
                  
    
            
                        memberList.Sort(comparison)

    Sunday, November 29, 2020 6:13 PM

Answers

  • User-939850651 posted

    Hi peterthegreat,

    When sorting the collection of class objects, the class needs to implement the IComparable interface and define the default comparator.

    As the example in the official document demonstrates, even if the compared attribute values are equal, it does not mean that they are the same class object. It will use the Equals() function to determine.

    Naturally, you need to define the Equals() function in class.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2020 6:06 AM
  • User475983607 posted

    What i meant was a comparison function. I dont have a class other than the membershipuser that is sorted using the comparison as an example only. i dont posses the full code.

    I was wondering how i could sort table bound to this class?

    Your question is very difficult to understand.  The MembershipUser is a single type that represents an application user.  If a MembershipUser is added to an array (or List) with other MembershipUser types then you have an an collection of MembershipUser types.  

    If you are trying to sort a list of MembershipUser types, then you get to craft a comparer.  The framework has no idea how to sort a complex type like the MembershipUser.  You have to tell the code how to sort; firstname, lastname, phone number, ID etc.    LINQ will also work.

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.comparer-1?view=netframework-4.8

    If the above suggestions are not not answering your question, can you explain the actual problem you are trying to solve?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 7, 2020 2:42 PM

All replies

  • User-939850651 posted

    Hi peterthegreat,

    When sorting the collection of class objects, the class needs to implement the IComparable interface and define the default comparator.

    As the example in the official document demonstrates, even if the compared attribute values are equal, it does not mean that they are the same class object. It will use the Equals() function to determine.

    Naturally, you need to define the Equals() function in class.

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2020 6:06 AM
  • User303363814 posted

    The comparison function determines whether one element is greater than. equal to or less than another.  You can supply different functions depending on how you want to sort the elements

    You might want an alphabetical list by Username, as you have.

    You might want an alphabetical list by LastName - change the comparison function to use that property.

    You might want a numerical list by ascending Balance - change the comparison function.  descending?  A different comparison function.

    Sort does the basic swapping but needs to know what you mean by less than for this particular operation.  That's the purpose of the comparison function

    Tuesday, December 1, 2020 8:21 PM
  • User1909155429 posted

    is there a way of determine which interface a collection can use? I am operating a membershipuser class and that does not accept the default,so having to implement a custom comparer and it is confusing to discover how?

    Sunday, December 6, 2020 9:51 PM
  • User303363814 posted

    What do you mean by "which interface a collection can use"

    An interface can be used if it is implemented by the object.

    An interface can be thought of as a promise.  It is a promise to have certain things (the properties that make up the interface) or to do certain things (the methods that make up the interface.  The compiler checks that the promise has been fulfilled.  If it is not fulfilled the code will not compile.

    I think you are asking which interfaces of a collection can you use.  You can use any that have been implemented.  The documentation for a class will tell you which interfaces that class implements.  If it is a Microsoft provided class then docs.microsoft.com will show you which interfaces any class implements.  If the class is from a third party then their documentation should list the implemented interfaces.  If it is your own class then you should know which interfaces you have written code to support!

    If you are asking about which interfaces you are allowed to support then their is no restriction that I know of.  However, many interfaces would not make sense (a Person class is unlike to want to implement IEnumerable<T>, for example - it just doesn't make sense)

    Do you have a concrete problem?  Show your code

    Sunday, December 6, 2020 10:27 PM
  • User1909155429 posted

    What i meant was a comparison function. I dont have a class other than the membershipuser that is sorted using the comparison as an example only. i dont posses the full code.

    I was wondering how i could sort table bound to this class?

    Monday, December 7, 2020 2:16 PM
  • User475983607 posted

    What i meant was a comparison function. I dont have a class other than the membershipuser that is sorted using the comparison as an example only. i dont posses the full code.

    I was wondering how i could sort table bound to this class?

    Your question is very difficult to understand.  The MembershipUser is a single type that represents an application user.  If a MembershipUser is added to an array (or List) with other MembershipUser types then you have an an collection of MembershipUser types.  

    If you are trying to sort a list of MembershipUser types, then you get to craft a comparer.  The framework has no idea how to sort a complex type like the MembershipUser.  You have to tell the code how to sort; firstname, lastname, phone number, ID etc.    LINQ will also work.

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.comparer-1?view=netframework-4.8

    If the above suggestions are not not answering your question, can you explain the actual problem you are trying to solve?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 7, 2020 2:42 PM
  • User1909155429 posted

    Thanks that is very helpful. but the examples only show how to sort list using multiple properties.How would that work for individual columns in gridview? 

    Friday, December 11, 2020 3:12 PM
  • User475983607 posted

    Thanks that is very helpful. but the examples only show how to sort list using multiple properties.How would that work for individual columns in gridview? 

    I do not understand your response. 

    I assume you have a list of MembershipUsers.  The MembershipUser properties are the the GridView columns.  You can sort one column or several.  It is totally up to you.  

    Friday, December 11, 2020 3:25 PM
  • User1909155429 posted

    The interface in the example sorts using three columns! i want to pass individual column headers to the sort function at a time,which appears confusing given the examples shown?

    Tuesday, December 15, 2020 1:55 PM
  • User475983607 posted

    The interface in the example sorts using three columns! i want to pass individual column headers to the sort function at a time,which appears confusing given the examples shown?

    Why does it matter if the example sorts by three properties?  Simply remove two of the conditions. 

    public class BoxLengthFirst : Comparer<Box>
    {
        // Compares by Length, Height, and Width.
        public override int Compare(Box x, Box y)
        {
            if (x.Length.CompareTo(y.Length) != 0)
            {
                return x.Length.CompareTo(y.Length);
            }
            else
            {
                return 0;
            }
        }
    }

    Tuesday, December 15, 2020 2:20 PM