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 4475
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!
Plater 7,872
Recognized Expert Expert
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 Guid.NewGuid().ToString() is
appended to make sure the key is unique in case of duplicated data.
|
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 key-value pairs
of UserID - DisplayName for a whole bunch of user objects. The UserID
is simply a long, the DisplayName is something like, "Jones, Bill". I
want the SortedList to sort the names alphabetically, rather than by
|
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 add to the list, it
only seems to sort alphabetically.
Thanks
|
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 that I could make the key for my sorted
list the LastWriteTime of the file.
Apart from this i'm at a real loose end.
Any clues anyone?
| |
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 poemfile As String In poemfiles
Poems.Add(System.IO.File.OpenText(poemfile).ReadLine(), poemfile)
|
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<>, both works.
private void Form1_Load(object sender, EventArgs e)
{
System.Collections.SortedList QA1 = new
System.Collections.SortedList();
|
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("??", "??")
Debugging this peace of code I have notice that '??' appears as first item,
|
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 SortedList like this:
class OtherSortedList : SortedList
{
.....
int GetTotalAmountOfAllItems()....
}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |