473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 6170
"Johannes" <an*******@discussions.microsoft.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.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);
}
}

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");

foreach (DictionaryEntry de in numericList)
Console.WriteLine(de.Key + ", " + de.Value);
}
}
}
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2
Johannes <an*******@discussions.microsoft.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.com>
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
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...
2
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: ...
4
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...
2
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...
4
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<>,...
4
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...
1
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:...
2
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...
4
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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,...
0
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...

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.