sort sortedlist question

T

Tem

I have a sorted list
SortedList<string, double> list

I need help sorting this list by value (double) in descending order
I know i can write my own comparison function but im looking for a simple
solution. i can use .net 35

Thx

Tem
 
J

Jon Skeet [C# MVP]

I have a sorted list
SortedList<string, double> list

I need help sorting this list by value (double) in descending order
I know i can write my own comparison function but im looking for a simple
solution. i can use .net 35

By the definition of a SortedList, the entries are always sorted by
key. Now, do you just need the values (the doubles) in a sorted form,
or do you need the keys as well? Either way, LINQ to Objects and the
"OrderByDescending" operator is likely to be the way forward.

Jon
 
A

AliRezaGoogle

By the definition of a SortedList, the entries are always sorted by
key. Now, do you just need the values (the doubles) in a sorted form,
or do you need the keys as well? Either way, LINQ to Objects and the
"OrderByDescending" operator is likely to be the way forward.

Jon

As microsoft has implemented Compare function of String type like a
built-in there is no way except to override it. So you should write
your own comparer class.
Alireza
 
T

Tem

I need to sort the values (double) and still able to access the keys.
can I create a new sortedlist and just swap the keys and values in the
original sortedlist?
 
J

Jon Skeet [C# MVP]

Tem said:
I need to sort the values (double) and still able to access the keys.
can I create a new sortedlist and just swap the keys and values in the
original sortedlist?

How do you need to access the keys? Do you actually need them sorted as
well?

Do you need to have sorted access to the values on a regular basis, or
just once occasionally? (You could sort when you needed them if it's
the latter.)
 
T

Tem

How do you need to access the keys? Do you actually need them sorted as
SortedList<string, double> students;
string is their name
double is their GPA
student's name and his GPA are always together (KeyPair)

I need to get a list of names ranked by their GPAs


Just need to sort it once.
Thanks
 
J

Jon Skeet [C# MVP]

SortedList<string, double> students;
string is their name
double is their GPA
student's name and his GPA are always together (KeyPair)

I need to get a list of names ranked by their GPAs

Just need to sort it once.

In that case, given that SortedList<TKey,TValue> implements
IEnumerable<KeyValuePair<TKey,TValue>> I'd just do something like:

grades.OrderBy(entry => entry.Value)

That will return an IEnumerable<KeyValuePair<string,double>> which you
can then convert into a list (call ToList) or just iterate over with
foreach.

By the way, I'd stongly recommend using decimal instead of double for
a GPA.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top