SortedList!! 
Author Message
 SortedList!!

Hi folks, By default SL reorders keys in ascending order...can you make the
keys descending ? Can someone post some snippet?

TIA

--
*************
Best Regards
Satch
*************



Tue, 02 Nov 2004 02:54:38 GMT  
 SortedList!!
Satch,

    If you want to make the keys descending, then I would pass an object
that implements IComparable into the constructor.  This IComparable would
forward all calls to the default comparer (exposed by Comparer.Default).
However, the return value is negated.  This will produce a list in
descending order.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Hi folks, By default SL reorders keys in ascending order...can you make
the
> keys descending ? Can someone post some snippet?

> TIA

> --
> *************
> Best Regards
> Satch
> *************



Tue, 02 Nov 2004 04:45:53 GMT  
 SortedList!!
some code snippet please!!! no clue how to implement the interface....

Thanks



Quote:
> Satch,

>     If you want to make the keys descending, then I would pass an object
> that implements IComparable into the constructor.  This IComparable would
> forward all calls to the default comparer (exposed by Comparer.Default).
> However, the return value is negated.  This will produce a list in
> descending order.

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET MVP]



> > Hi folks, By default SL reorders keys in ascending order...can you make
> the
> > keys descending ? Can someone post some snippet?

> > TIA

> > --
> > *************
> > Best Regards
> > Satch
> > *************



Tue, 02 Nov 2004 06:10:11 GMT  
 SortedList!!
Satch,
    Here's one approach - providing a IComparer in the SortedList constructor.
I believe Nicholas either meant to refer to this, or meant that the keys themselves should implement
IComparable.

using System;
using System.Collections;
class SortDesc{
 class DescComparer:IComparer{
  public int Compare(object a,object b){
   return (-1)*Comparer.Default.Compare(a,b);//can also use CaseInsensitiveComparer.Default if needed
  }
 }
 public static void Main() {
  SortedList sl=new SortedList(new DescComparer());
  sl.Add("B","world");
  sl.Add("C","Hello ");
  sl.Add("A","!");
  for(int i=0;i<sl.Count;i++)
  Console.Write(sl.GetByIndex(i));
 }

Quote:
}

HTH,

--
Yizhaq Shmaayahoo

Quote:

> some code snippet please!!! no clue how to implement the interface....

> Thanks



> > Satch,

> >     If you want to make the keys descending, then I would pass an object
> > that implements IComparable into the constructor.  This IComparable would
> > forward all calls to the default comparer (exposed by Comparer.Default).
> > However, the return value is negated.  This will produce a list in
> > descending order.

> >     Hope this helps.

> > --
> >                - Nicholas Paldino [.NET MVP]



> > > Hi folks, By default SL reorders keys in ascending order...can you make
> > the
> > > keys descending ? Can someone post some snippet?

> > > TIA

> > > --
> > > *************
> > > Best Regards
> > > Satch
> > > *************



Tue, 02 Nov 2004 09:59:10 GMT  
 SortedList!!
Thank you my friend.


Quote:
> Satch,
>     Here's one approach - providing a IComparer in the SortedList
constructor.
> I believe Nicholas either meant to refer to this, or meant that the keys

themselves should implement
Quote:
> IComparable.

> using System;
> using System.Collections;
> class SortDesc{
>  class DescComparer:IComparer{
>   public int Compare(object a,object b){
>    return (-1)*Comparer.Default.Compare(a,b);//can also use

CaseInsensitiveComparer.Default if needed
Quote:
>   }
>  }
>  public static void Main() {
>   SortedList sl=new SortedList(new DescComparer());
>   sl.Add("B","world");
>   sl.Add("C","Hello ");
>   sl.Add("A","!");
>   for(int i=0;i<sl.Count;i++)
>   Console.Write(sl.GetByIndex(i));
>  }
> }

> HTH,

> --
> Yizhaq Shmaayahoo



- Show quoted text -

Quote:
> > some code snippet please!!! no clue how to implement the interface....

> > Thanks


wrote

> > > Satch,

> > >     If you want to make the keys descending, then I would pass an
object
> > > that implements IComparable into the constructor.  This IComparable
would
> > > forward all calls to the default comparer (exposed by
Comparer.Default).
> > > However, the return value is negated.  This will produce a list in
> > > descending order.

> > >     Hope this helps.

> > > --
> > >                - Nicholas Paldino [.NET MVP]



> > > > Hi folks, By default SL reorders keys in ascending order...can you
make
> > > the
> > > > keys descending ? Can someone post some snippet?

> > > > TIA

> > > > --
> > > > *************
> > > > Best Regards
> > > > Satch
> > > > *************



Tue, 02 Nov 2004 23:09:20 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. SortedList Collection

2. Odd problem with System.Collections.SortedList

3. SortedList, searching for items efficiently

4. deriving from SortedList?

5. SortedList with duplicate items?

6. SortedList sorted by value.

7. Please Help with SortedList!!!

8. SortedList, Why Parenthesis instead of Brackets?

9. Collections.SortedList

10. SortedList Key as DateTime Class?

11. mc++: how to iterate over values of a SortedList??

 

 
Powered by phpBB® Forum Software