I thought I would be tricky and force the SortedList to not sort using a custom IComparer, I used the following code, but when the list is sorting using the custom sort the accessors become unusable and throw exceptions. I have since discovered that the accessors use the IComparer to find things and since it is set at -1 it gets a null return value, however the foreach loops dont use it. I must preserve the insert order, I don't want the sortinglist to sort, is there any way or type to accomplish this? A ListDictionary does not have the accessors I need. I also need it to be Case-Sensitive meaning for keys I have some that are 'r99' and 'R99' and that they are not the same. (Loading and XML file and XML is Case-Sensitive)
Thanks
Sample Application: -
using System;
-
using System.Collections;
-
-
namespace Example
-
{
-
// An implementation of IComparer.
-
public class Int32ComparerClass : IComparer
-
{
-
public int Compare(object x, object y)
-
{
-
// This code assumes neither x and y are null,
-
// and they both can be successfully converted to Int32s.
-
//return Convert.ToInt32(x) - Convert.ToInt32(y);
-
return -1;
-
}
-
}
-
-
public class Test
-
{
-
[STAThread]
-
public static void Main()
-
{
-
SortedList numericList =
-
new SortedList(new Int32ComparerClass());
-
numericList.Add("10", "Ten");
-
numericList.Add("11", "Eleven");
-
numericList.Add("1", "One");
-
numericList.Add("2", "Two");
-
numericList.Add("3", "Three");
-
numericList.Add("0", "0");
-
numericList.Add("r99", "r99");
-
numericList.Add("R99", "R99");
-
-
foreach (DictionaryEntry de in numericList)
-
Console.WriteLine(de.Key + ", " + de.Value);
-
-
//WILL THROW EXCEPTION
-
Console.WriteLine(numericList["1"].ToString());
-
-
Console.WriteLine();
-
-
SortedList xxx =
-
new SortedList();
-
xxx.Add("10", "Ten");
-
xxx.Add("11", "Eleven");
-
xxx.Add("1", "One");
-
xxx.Add("2", "Two");
-
xxx.Add("3", "Three");
-
xxx.Add("0", "0");
-
xxx.Add("r99", "r99");
-
xxx.Add("R99", "R99");
-
-
-
foreach (DictionaryEntry de in xxx)
-
Console.WriteLine(de.Key + ", " + de.Value);
-
-
//IS OK
-
Console.WriteLine(xxx["1"].ToString());
-
-
}
-
}
-
}
-
3 4452
Hi, thanks for taking the time out to write a sample program. The code sample you provided should work fine using a ListDictionary. Which accessors are missing from ListDictionary that make it unusable for you?
Thanks, Apparently I wasn't paying attention, it works fine with a list dictionary! Thanks!
I was going to say, you are using a SortedList, but not wanting Sorting and that there must be a better alternative. Glad you found it.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Zeng |
last post by:
Would someone know why SortedList doesn't work for these two keys?
System.Collections.SortedList.Add(Object key, Object value) +221
My code looks something like below, the...
|
by: gerrod |
last post by:
Hi -
Does anyone know a way to created a SortedList (in the
System.Collections namespace) that will sort on VALUES instead of
KEYS... ?
The scenario is this - I have a SortedList containing...
|
by: Johannes |
last post by:
I've read that the SortedList object can sort its elements
in alphabetical or numerical order. If this is correct,
how can I set the sort order to numerical.
Regardless of the key/value pairs I...
|
by: Michael C |
last post by:
Hi all,
I'm using a SortedList to store data, and want the keys to be compared in
case insensitive order, so that
mySList
is the same as
mySList
|
by: CodeRazor |
last post by:
Hi,
I want to retrieve a sorted list of files, ordered by LastWriteTime. I know
I can implement IComparer, but I don't know how or what this means. I know a
sortedlist has objects and keys and...
|
by: Nathan Sokalski |
last post by:
I am trying to make a SortedList using the following code:
Dim Poems As New SortedList(47)
Dim poemfiles As String() =
System.IO.Directory.GetFiles(Server.MapPath("poetry/poems/"))
For Each...
|
by: SHEBERT |
last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox
and a generic SortedList<that does not works as a datasource to the
ComboBox.
Why? If I use List and generic List<>,...
|
by: Antonio Paglia |
last post by:
Hello. I have tried to insert this items into a SortedList.
dic = New SortedList
dic.Add("<<", "<<")
dic.Add("==", "==")
dic.Add(">>", ">>")
dic.Add("@@", "@@")
dic.Add("??", "??")
...
|
by: semedao |
last post by:
Hi,
I want to implement list of key-values that can be sort by 2 ways.
let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |