473,624 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SortedList question

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
Nov 15 '05 #1
3 6184
"Johannes" <an*******@disc ussions.microso ft.com> wrote in
news:02******** *************** *****@phx.gbl:
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.


Johannes,

You can implement the IComparer interface to sort the keys in your
SortedList:
using System;
using System.Collecti ons;

namespace Example
{
// An implementation of IComparer.
public class Int32ComparerCl ass : 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);
}
}

public class Test
{
[STAThread]
public static void Main()
{
SortedList numericList =
new SortedList(new Int32ComparerCl ass());
numericList.Add ("10", "Ten");
numericList.Add ("11", "Eleven");
numericList.Add ("1", "One");
numericList.Add ("2", "Two");
numericList.Add ("3", "Three");

foreach (DictionaryEntr y de in numericList)
Console.WriteLi ne(de.Key + ", " + de.Value);
}
}
}
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2
Johannes <an*******@disc ussions.microso ft.com> wrote:
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.


Give the SortedList constructor an appropriate IComparer which compares
objects in the way you want them sorted, and it can sort them any way
you want.

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

Chris, thanks for the sample code!

-----Original Message-----
I've read that the SortedList object can sort its elementsin 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
.

Nov 15 '05 #4

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

Similar topics

1
20524
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
2
2144
by: arby | last post by:
Hi There. I have a SortedList collection that I add objects of a custom class to. Rather then explain my app, my question is pretty general so.... Using a simple clsDog: *************************** clsDog boomer = new clsDog(); clsDog spot = new clsDog();
4
1211
by: J L | last post by:
I have a sortedlist (ConflictList) that contains a string identifier (ShipmentNumber) for the key and a structure (AppointmentInfo) for the value. The structure memebers are strings, dates and intgers (i.e. no objects). I pass this sorted list by reference to a function. And do the following type of loop dim i as integer for i = 0 to ConflictList.count - 1
2
2040
by: Prez | last post by:
I started writing .net code yesterday and I am grasping it well enough. I have a few questions about SortedLists. I am using managed C++ if that makes any difference. Of the examples I have seen it looks like the Sorted List does not use the ^ while other variables do use the ^. I have used the SortedList with the ^ and gcnew and it seems to work fine. So the question is...
4
10117
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();
4
1535
by: sck10 | last post by:
Hello, I have a method in my codefile that builds a sorted list (see CodeFile). I am trying to create a class that does the same thing (see App_Code). CodeFile =================================== public void PostSearch(string HRID) { SortedList PostSearchList = new SortedList();
1
3897
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash: System::Collections::SortedList, System::Collections::Generic::SortedList, using namespace System::Collections; using namespace System::Collections::Generic; Below is a working version of the generic template SortedList, which
2
1327
by: active | last post by:
This is where a different thread ended. The subject of this query is quite different from that of the other thread so I thought I should start a new thread. I'm using Private mItemList As SortedList(Of String, StringWithInteger) To populate a generic CombBox StringWithInteger has two properties Str and Value
4
4311
by: Michael Nesslinger | last post by:
Hello, i am looking for an easy way to do a "RemoveAll(Predicate<Tmatch)" for a SortedList like it is possible for a List. My first question is: Why is the Method not available for the SortedList. And my second one: How can it be done the best (fastest) way for a SortedList
6
2507
by: n3tx | last post by:
Hi! I have a problem with sortedlist, i guess i dont understand how it works. I have a method called GetPublishingPlaces that returns an IList<PublishingPlace> (ex. contains 11 rows) I want to sort the list in different orders, I dont know if this is something that SortedList do ? i have a property in the PublishingPlace called Place, name, exposure
0
8175
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
8680
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...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7168
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
6111
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...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
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.