473,387 Members | 1,569 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,387 software developers and data experts.

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 20479
-----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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...

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.