Re: sort sortedlist question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Skeet [C# MVP]

    Re: sort sortedlist question

    Tem <tem1232@yahoo. comwrote:
    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.)

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com
  • Tem

    #2
    Re: sort sortedlist question

    How do you need to access the keys? Do you actually need them sorted as
    well?
    SortedList<stri ng, doublestudents;
    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

    "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
    news:MPG.22c0ef ebfeefada9d87@m snews.microsoft .com...
    Tem <tem1232@yahoo. comwrote:
    >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.)
    >
    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: sort sortedlist question

      On Jun 16, 11:05 pm, "Tem" <tem1...@yahoo. comwrote:
      How do you need to access the keys? Do you actually need them sorted as
      well?
      >
      SortedList<stri ng, doublestudents;
      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 ,TValueimplemen ts
      IEnumerable<Key ValuePair<TKey, TValue>I'd just do something like:

      grades.OrderBy( entry =entry.Value)

      That will return an IEnumerable<Key ValuePair<strin g,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

      Comment

      Working...