473,498 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SortedList - Sort by VALUE (not KEY)

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
increasing order of UserIDs.

The simple answer is to use the DisplayName as a key, and UserID as a
value. However this barfs if I have multiple people with the same
DisplayName, as you can't have duplicate keys in a SortedList.

I know that I can override the IComparer that the SortedList uses for
comparisons, however, logistically I cannot think of a way to create
an IComparer that will be able to access the VALUES of the SortedList
collection. The problem is that I MUST supply the IComparer at
construction time of the SortedList; however the IComparer needs to
know about the SortedList so that when it is supplied with 2 keys to
sort, it can then access the values of the collection to work out
which comes before the other. And of course, I can't do something like
this -

SortedList sList = new SortedList(new MyComparer(sList));

for (hopefully) obvious reasons...

The other thing I tried was creating a "wrapper class" like this -

class SortedUserList : IComparer {
private SortedList userList = null;

public SortedList UserList {
get {
if (userList == null) {
userList = new SortedList(this);
}

return userList;
}
}

public SortedUserList() {
}

#region IComparer Members

public int Compare(object x, object y) {
string first = UserList[x] as string;
string second = UserList[y] as string;

return first.CompareTo(second);
}

#endregion
}

But this also didn't work - I got a Stack Overflow when I tried to add
the second user into the list.

I'm really hoping that there is a simple solution to this problem.

Thanks for your help,
gerrod
Nov 15 '05 #1
1 20493
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

No you can't just pass in a Comparator, because in SortedList, it is
automatically applied to the keys instead of the values, no matter what
you do (and that's the way it is supposed to work anyway...)

An easy way would be to preserve the link between the key and the value
yourself, e.g.: in a struct, like a short program below, and then make
it implement IComparable to sort it based on user names. Then, a simple
ArrayList will do from there (see below)

using System;
using System.Collections;

class MainClass
{
~ public static void Main(string[] args) {
~ ArrayList users = new ArrayList();

~ users.Add(new User(1000, "Set"));
~ users.Add(new User(1004, "List"));
~ users.Add(new User(1002, "Gerrod"));
~ users.Add(new User(1003, "Map"));

~ users.Sort();

~ foreach (User user in users) {
~ Console.WriteLine(user.Id + ", " + user.Name);
~ }
~ }
}

struct User : IComparable {
~ private long _id;
~ private string _name;

~ public User(long id, string name) {
~ _id = id;
~ _name = name;
~ }

~ public long Id {
~ get {
~ return _id;
~ }
~ }

~ public string Name {
~ get {
~ return _name;
~ }
~ }

~ public int CompareTo(object obj) {
~ if(obj == null) {
~ return 1;
~ }
~ User that = (User)obj;
~ return this._name.CompareTo(that._name);
~ }
}
gerrod wrote:

| 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
| increasing order of UserIDs.
|
| The simple answer is to use the DisplayName as a key, and UserID as a
| value. However this barfs if I have multiple people with the same
| DisplayName, as you can't have duplicate keys in a SortedList.
|
| I know that I can override the IComparer that the SortedList uses for
| comparisons, however, logistically I cannot think of a way to create
| an IComparer that will be able to access the VALUES of the SortedList
| collection. The problem is that I MUST supply the IComparer at
| construction time of the SortedList; however the IComparer needs to
| know about the SortedList so that when it is supplied with 2 keys to
| sort, it can then access the values of the collection to work out
| which comes before the other. And of course, I can't do something like
| this -
|
| SortedList sList = new SortedList(new MyComparer(sList));
|
| for (hopefully) obvious reasons...
|
| The other thing I tried was creating a "wrapper class" like this -
|
| class SortedUserList : IComparer {
| private SortedList userList = null;
|
| public SortedList UserList {
| get {
| if (userList == null) {
| userList = new SortedList(this);
| }
|
| return userList;
| }
| }
|
| public SortedUserList() {
| }
|
| #region IComparer Members
|
| public int Compare(object x, object y) {
| string first = UserList[x] as string;
| string second = UserList[y] as string;
|
| return first.CompareTo(second);
| }
|
| #endregion
| }
|
| But this also didn't work - I got a Stack Overflow when I tried to add
| the second user into the list.
|
| I'm really hoping that there is a simple solution to this problem.
|
| Thanks for your help,
| gerrod
- --
Ray Hsieh (Djajadinata)
ray underscore usenet at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/nIkiwEwccQ4rWPgRAqn+AJ402SGnegA2+hvAOFQf+3PvfI5yiQ CfYAk4
cXPzlQMWMVXcpDbSsNK103w=
=tjwy
-----END PGP SIGNATURE-----

Nov 15 '05 #2

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

Similar topics

3
6174
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...
1
1524
by: Vai2000 | last post by:
Hi All, Is there a way to custom sort the SortedList by its value...I know how to do with keys by passing a special Comparable Class...but how do I sort it based on Values? TIA
3
2806
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
6
1859
by: Jose Jarabo | last post by:
Hello, thanks in advance for any and all replies. I am not sure how to categorize this, either a bug or something else but here is my problem. I am testing with 2 elements on a sorted list. If...
2
3064
by: Pekka | last post by:
Could somebody say why the piece of code below does not work? My purpose is to renumber keys in a SortedList (after removal of an item) so that the keys would always contain an unbroken sequence of...
6
1531
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("??", "??") ...
4
6749
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...
1
3885
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:...
3
4457
dcharnigo
by: dcharnigo | last post by:
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...
0
7125
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
7004
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...
0
7167
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
7208
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...
1
6890
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...
0
5464
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,...
0
4593
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...
0
3095
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...
0
292
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...

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.