none
How to sort an IList RRS feed

  • Question

  • I have an IList<Double?> and I need to sort it. IList doesn't have its own .Sort method.
    Tuesday, July 29, 2008 7:22 PM

Answers

  • never mind, there was another section of the code that prvented me from making it an IList<Double?> so I used List<Double?> and apparently that works.
    • Marked as answer by acfalconhawk Tuesday, July 29, 2008 7:54 PM
    Tuesday, July 29, 2008 7:54 PM
  •  I would strongly recommend learning the difference between a class and an interface.  It seems you have some confusion regarding this. 

    This is of course very true but the links you've provided, very regretfully, notwistanding the fact that they are from wikipedia, are less than helpful. They are more on the confusing side as far as the Microsoft stuff goes. I've noticed it on a number of occasions that MS concepts get a very short shrift if at all when people write about computing. You can read about Linux, Unix and devil knows what but not what actually matters to you today and for better or worse it is all Microsoft. It is because the writers are mostly campus geeks who have nothing else to do.

    I think Interface concept is very hard to grasp for a beginner. It can be best illustrated thru engineering. DELL for instance offers you a "desktop" or a "laptop" of certain model and when you start thinking about purchasing a box you are dealing with an "Interface" or rather a set of interfaces costing you perhaps $500. It is a generic concept of a machine that can have 100 various components potentially with many variations and some of them you may reject. Then you narrow them down to what you want and you've got your "class" costing you $3000. It is a computer but much more specific. You've got a CPU with 3.0 Ghz, RAM 2Gbs, etc. At the same time you can purchase individual components (Interfaces) separately and use them for your own ideas.

    Getting down to IList vs List.

    IList is an Interface. By definition an interface provides only signatures of methods, delegates, events and properties. As a rule you don't use interfaces directly although you can. Classes are built from them.

    IList has 

    Methods:
    Add, Clear, Contains, CopyTo, GetEnumerator, IndexOf, Insert, Remove, RemoveAt  ( 9 methods)

    Extension Methods    
    AsQueryable, Cast, OfType   ( 3 extension methods )

    Properties:
    Count, IsFixedSize,I sReadOnly, IsSynchronized, Item, SyncRoot  ( 6 properties )

    By contrast,

    List
    <T> (System.Collection.Generic) strongly typed Class has 35 public methods, 2 protected methods and 3 properties because it inherits from 14 explicit Interface implementations and IList is just one of them.

    According to MS 35 classes implement IList (ironcally, List is not listed among them) which means that IList is used to build 35 classes, perhaps more. Among them are ArrayList, 3 classes related to ListBox, 6 classes related to ListView and 3 classes related to Sql Server objects.


    http://msdn.microsoft.com/en-us/library/system.collections.ilist(VS.71).aspx


    AlexB
    Tuesday, July 29, 2008 9:46 PM

All replies

  • You could copy the IList to an array and then use Array.Sort.  Although that probably is not the ideal solution.
    -Matt
    Tuesday, July 29, 2008 7:35 PM
    Moderator
  • never mind, there was another section of the code that prvented me from making it an IList<Double?> so I used List<Double?> and apparently that works.
    • Marked as answer by acfalconhawk Tuesday, July 29, 2008 7:54 PM
    Tuesday, July 29, 2008 7:54 PM
  • In general you should avoid dependencies on concrete classes and instead depend on interfaces. In this case, it is good to depend on IList rather than one of the IList implementations (namely List). If you are using C# 3 you can do this to sort through the IList interface:

    IList<int> numbers = new List<int> {9, 2, 7, 5, 1};

    var tmp = numbers.OrderBy(x => x);
    Tuesday, July 29, 2008 8:08 PM
  • acfalconhawk,

    I would strongly recommend learning the difference between a class and an interface.  It seems you have some confusion regarding this. 

    http://en.wikipedia.org/wiki/Interface_%28computer_science%29

    http://en.wikipedia.org/wiki/Class_%28computer_science%29

    David Morton - Consultant - Catapult Systems - Houston
    Tuesday, July 29, 2008 8:09 PM
    Moderator
  •  I would strongly recommend learning the difference between a class and an interface.  It seems you have some confusion regarding this. 

    This is of course very true but the links you've provided, very regretfully, notwistanding the fact that they are from wikipedia, are less than helpful. They are more on the confusing side as far as the Microsoft stuff goes. I've noticed it on a number of occasions that MS concepts get a very short shrift if at all when people write about computing. You can read about Linux, Unix and devil knows what but not what actually matters to you today and for better or worse it is all Microsoft. It is because the writers are mostly campus geeks who have nothing else to do.

    I think Interface concept is very hard to grasp for a beginner. It can be best illustrated thru engineering. DELL for instance offers you a "desktop" or a "laptop" of certain model and when you start thinking about purchasing a box you are dealing with an "Interface" or rather a set of interfaces costing you perhaps $500. It is a generic concept of a machine that can have 100 various components potentially with many variations and some of them you may reject. Then you narrow them down to what you want and you've got your "class" costing you $3000. It is a computer but much more specific. You've got a CPU with 3.0 Ghz, RAM 2Gbs, etc. At the same time you can purchase individual components (Interfaces) separately and use them for your own ideas.

    Getting down to IList vs List.

    IList is an Interface. By definition an interface provides only signatures of methods, delegates, events and properties. As a rule you don't use interfaces directly although you can. Classes are built from them.

    IList has 

    Methods:
    Add, Clear, Contains, CopyTo, GetEnumerator, IndexOf, Insert, Remove, RemoveAt  ( 9 methods)

    Extension Methods    
    AsQueryable, Cast, OfType   ( 3 extension methods )

    Properties:
    Count, IsFixedSize,I sReadOnly, IsSynchronized, Item, SyncRoot  ( 6 properties )

    By contrast,

    List
    <T> (System.Collection.Generic) strongly typed Class has 35 public methods, 2 protected methods and 3 properties because it inherits from 14 explicit Interface implementations and IList is just one of them.

    According to MS 35 classes implement IList (ironcally, List is not listed among them) which means that IList is used to build 35 classes, perhaps more. Among them are ArrayList, 3 classes related to ListBox, 6 classes related to ListView and 3 classes related to Sql Server objects.


    http://msdn.microsoft.com/en-us/library/system.collections.ilist(VS.71).aspx


    AlexB
    Tuesday, July 29, 2008 9:46 PM
  • David, your answer is not helping at all. It's too bad we can't vote down.
    David Pokluda (MSFT)
    http://blog.pokluda.com
    Thursday, July 1, 2010 1:03 AM