4.0 [RESOLVED] SortedList, Compare, output argument-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] SortedList, Compare, output argument

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    90

    Resolved [RESOLVED] SortedList, Compare, output argument

    I used SortedList to add new items. How to put ref arguments in Compare function?
    When the strings ignored case are the same, always add the first similar item.
    e.g. I used string.Compare and ignore the case to add the following strings:
    "Davis"
    "DAVIS"
    "davis"

    They are equal, so I want to add text into SortedList as is:
    Davis,
    Davis
    Davis


    Code:
    SortedList1 = new SortedList(new DegreeComparer()); 
    SortedList1.Add(text, num);
    
    internal class DegreeComparer : IComparer
    {
    public int Compare(ref object valueX, ref object valueY)
    {
       
        //...
        If (string.Compare(Convert.ToString(valueX), Convert.ToString(valueY), true) == 0)
        {
            valueY = valueX;
            Return 1;
    
        }
        //...
    
    }
    }
    In above code, I got error "does not implement interface member 'System.Collections.IComparer.Compare(object, object)" because the Compare function cannot allow ref argument. How to achieve the result and avoid the error?
    Last edited by DaveDavis; Jan 11th, 2018 at 05:58 AM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,414

    Re: SortedList, Compare, output argument

    Your question doesn't really make much sense. You can't declare those parameters 'ref' because the interface isn't declared that way but doing so would make no sense anyway. The point of that method is to compare two values. It should not have any side-effects. Implementing side-effects is just bad coding.

    A SortedList requires unique keys. If you're going to use a SortedList then you need to provide unique keys and if you're not going to provide unique keys then don't use a SortedList. Perhaps you should provide a FULL and CLEAR explanation of what you're trying to achieve and then we can suggest how you might achieve it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    90

    Re: SortedList, Compare, output argument

    Quote Originally Posted by jmcilhinney View Post
    Your question doesn't really make much sense. You can't declare those parameters 'ref' because the interface isn't declared that way but doing so would make no sense anyway. The point of that method is to compare two values. It should not have any side-effects. Implementing side-effects is just bad coding.
    It is an experimental trail to reach the destination. I agree it is not practical.
    A SortedList requires unique keys. If you're going to use a SortedList then you need to provide unique keys and if you're not going to provide unique keys then don't use a SortedList. Perhaps you should provide a FULL and CLEAR explanation of what you're trying to achieve and then we can suggest how you might achieve it.
    SortedList can add the same items if we force the equivalents (0) to 1 or -1 in ICompare.
    I used SortedList to do multiple sorting. I need such tricks to find the grouped same items for further sorting (e.g. Davis, DAVIS,davis).
    1, Davis, 2500, 2
    2, Steve, 500, 1
    3, DAVIS, 2500, 1
    4, Peter, 2600, 2
    5, davis, 2800, 2
    1st sort Column 2:
    1, Davis, 2500, 2
    3, DAVIS, 2500, 1
    5, davis, 2800, 2
    4, Peter, 2600, 2
    2, Steve, 500, 1
    2nd sort Column 4:
    1, Davis, 2500, 1
    3, DAVIS, 2500, 2
    5, davis, 2800, 2
    4, Peter, 2600, 2
    2, Steve, 500, 1
    Edited:
    OK. Better question title is "how to change the Key on fly while SortedList adding the same items."
    Last edited by DaveDavis; Jan 11th, 2018 at 06:10 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    90

    Re: SortedList, Compare, output argument

    OK. I got it. I convert the adding string to LCase if I use String.Compare's ignoreCase option.

    Resolved.

    Code:
    SortedList1.Add((stringCompareMode == StringCompareModesEnum.StringCompareIgnoreCase ? text.ToLower() : text), num);

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.