473,782 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting Hashtable

Hello

I defined:
Hashtable chan1 = new Hashtable(new
CaseInsensitive HashCodeProvide r(CultureInfo.I nvariantCulture ), new
InvariantCompar er());

where:
public class InvariantCompar er : IComparer
{
public CompareInfo m_compareInfo;
internal static readonly InvariantCompar er Default = new
InvariantCompar er();

internal InvariantCompar er()
{
m_compareInfo = CultureInfo.Inv ariantCulture.C ompareInfo;
}

public int Compare(Object a, Object b)
{/* compare my objects here */
DscRecord ra = a as DscRecord;
DscRecord rb = b as DscRecord;
if (ra.minute < rb.minute)
return 1;
else if (ra.minute > rb.minute)
return -1;

if (ra.second < rb.second)
return 1;
else if (ra.second > rb.second)
return -1;

if (ra.ssecond < rb.ssecond)
return 1;
else if (ra.ssecond > rb.ssecond)
return -1;

return 0;
}
}

but when i read data from hashtable:
IDictionaryEnum erator myEnumerator = chan.GetEnumera tor();
while ( myEnumerator.Mo veNext() )
{
......
}

i receive them "unsorted". Why ?
Is there any other way to read records from HashTable which is sorted by
key ?

Thanx
Michal
Nov 17 '05 #1
1 7203
vertigo <nu**@com.pl> wrote:
I defined:
Hashtable chan1 = new Hashtable(new
CaseInsensitive HashCodeProvide r(CultureInfo.I nvariantCulture ), new
InvariantCompar er());
<snip>
i receive them "unsorted". Why ?
Because hashtables are fundamentally unsorted. They're maps, not lists.
Is there any other way to read records from HashTable which is sorted by
key ?


Well, you could use SortedList (which is actually a map - don't let the
poor naming deceive you) instead. That's what it's there for.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5228
by: JezB | last post by:
I have a hashtable (h) of class objects keyed by a string (k) - the class (c) object stored in each hastable entry has two attributes (a1 and a2). I want to iterate through the entries of the hashtable by ordering by one of the attributes of the class (a1 or a2). How can this easily be achieved ? I think I have to copy to an ArrayList then write an IComparer - but I'm not sure if this is the best way. Any advice appreciated.
0
5360
by: Rudolph Araujo | last post by:
Hi, I needed to build a data grid in which the first column has hyperlinks rather than simple text. I found the following code on this newsgroup and it works fine except for one problem. When I click the column headers all the columns sort except the hyperlink one. I checked the underlying dataview and that does get sorted so it seems like it is more of a display issue. I am guessing it has something to do with the hashtable used by the...
2
1636
by: Vidyanand Kulkarni | last post by:
Hello everybody, can anybody tell me how how he sorting on hashtable is done in c# thanking in advance with regards vidyanand kulkarni
6
7832
by: Fred Morrison | last post by:
Do you know of a way to load a hash table with random key/value pairs (e.g., 2/"Two",1/"One",3/"Three") and then iterate through the entries in "sorted" (key sequence) order (1/"One",2/"Two",3/"Three")? The following just returns them in the order they were loaded: Dim hshPrimaryKeyInfo as New Hashtable <snip code to Add entries to hash table> For Each hshEntry As DictionaryEntry In hshPrimaryKeyInfo
3
3771
by: Mark Gilkes | last post by:
Hi, I have an aspx page in which I am building a DataGrid control in the code-behind dynamically. The DataGrid is declared along with BoundColumns and bound to the datasource, then added to a panel control in the OnInit method of the aspx Page. I am trying to implement a bidirectional sorting. I have a SortCommand method which fires OK. I have followed the code example given on the 4GuysFromRolla site, which reads the value from...
11
7807
by: garyhoran | last post by:
Hi Guys, I have a collection that contains various attributes (stuff like strings, DateTime and Timespan) . I would like to access the collection in various orders at different points in the application ie sometimes I want to cycle through the values in the collection in DateTime order while at other times in TimeSpan order. Ideally I would like multiple keys - such as Timespan within DateTime order but maybe that is asking too much.
7
1286
by: cmdolcet69 | last post by:
Im trying to compare arrayindex slots in a array. I have an arraylist of size (n) and once all the arraylist slots are filled i would like to compare each slot so that they are all unique number. Example: (455,453,456,435,437,333) this a unique arraylist (455,435,455,455,422,222) this has multiple values and will not work.
2
1345
by: almurph | last post by:
H ieveryone, Can you help me please? I am trying to sort a hashtable but get the error: "Cannot implicity convert type void to System.Collections.ArrayList" I am doing the following: ****BEGIN CODE****
1
1186
by: almurph | last post by:
H ieveryone, Can you help me please? I am trying to sort a hashtable but get the error: "Cannot implicity convert type void to System.Collections.ArrayList" I am doing the following: ****BEGIN CODE****
0
9479
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,...
0
10311
Oralloy
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...
0
10146
jinu1996
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...
0
9942
tracyyun
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...
0
8967
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
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...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.