[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?
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.
Re: SortedList, Compare, output argument
Quote:
Originally Posted by
jmcilhinney
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.
Quote:
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).
Quote:
1, Davis, 2500, 2
2, Steve, 500, 1
3, DAVIS, 2500, 1
4, Peter, 2600, 2
5, davis, 2800, 2
1st sort Column 2:
Quote:
1, Davis, 2500, 2
3, DAVIS, 2500, 1
5, davis, 2800, 2
4, Peter, 2600, 2
2, Steve, 500, 1
2nd sort Column 4:
Quote:
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."
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);