I Need Help with sorting an arraylist (VB.net)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eLisHa

    I Need Help with sorting an arraylist (VB.net)

    i have an arraylist that gets it's values dynamiclly from the database, after
    paging, searching & more in the ASP.net file.
    What im trying to do, is sort the results that are sorted in the ArrayList,
    each time by another field.
    The ArrayList contains a list of a class which i built, that fits these
    results.
    I was told to use a IComparer with the sort method, after i try'ed just
    ArrayList1.Sort i got the following error:
    System.Argument Exception: At least one object must implement IComparable

    try'ed giving the Sort method an IComparable parameter, it still gives me
    the same error.
    How can i sort my ArrayList without building a full interface for it? (or is
    it the only way?)

    thanks in advance.
  • Jon Skeet [C# MVP]

    #2
    Re: I Need Help with sorting an arraylist (VB.net)

    eLisHa <eLisHa@discuss ions.microsoft. com> wrote:[color=blue]
    > i have an arraylist that gets it's values dynamiclly from the database, after
    > paging, searching & more in the ASP.net file.
    > What im trying to do, is sort the results that are sorted in the ArrayList,
    > each time by another field.
    > The ArrayList contains a list of a class which i built, that fits these
    > results.
    > I was told to use a IComparer with the sort method, after i try'ed just
    > ArrayList1.Sort i got the following error:
    > System.Argument Exception: At least one object must implement IComparable
    >
    > try'ed giving the Sort method an IComparable parameter, it still gives me
    > the same error.
    > How can i sort my ArrayList without building a full interface for it? (or is
    > it the only way?)[/color]

    Could you post a short but complete program which demonstrates the
    problem?

    See http://www.pobox.com/~skeet/csharp/complete.html for details of
    what I mean by that.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: I Need Help with sorting an arraylist (VB.net)

      eLisHa,

      You know that at Arraylist sort on MSDN is a complete sample that in my
      opinion describes your question.



      I hope this helps,

      Cor


      Comment

      • eLisHa

        #4
        A better explanation of what i need help in

        Ok then... about that MSDN article, yeah, ive read it lots of times... it
        never help me though.


        My web page works like this:
        [color=blue]
        >Page, a search results page, gets search crietenia as a QuerySting
        >BindData Sub, which adds the needed data to the asp.net controls. It mainly gets the full tables from the database and searchs in them for the requested text, etc. When a result is found, it is added to my global ArrayList, as a Discount. example:[/color]

        Public Class Discount
        Private iID As Integer, iName As String, iBus As String
        Sub New(sID As Integer, sName As String, sBus As String)
        ' This creats a new instance of the Discount class, gets all information
        needed about the discount.
        ' for instance:
        Me.iID = sID
        Me.iName = sName
        Me.iBus = sBus
        End Sub

        Public ReadOnly Property ID() As String
        Get
        Return iID
        End Get
        End Property


        Public ReadOnly Property Name() As String
        Get
        Return iName
        End Get
        End Property


        Public ReadOnly Property Bus() As String
        Get
        Return iBus
        End Get
        End Property



        End Class
        [color=blue]
        > To add a discount to the global ArrayList, i use the Add method:[/color]
        ArrayList1.Add( New Discount(drDisc ount("id"), drDiscount("typ e_name"),
        ("Business") ))
        [color=blue]
        >After the BindData() Finishes, there is a function for paging, which gets it pageId value from ViewState.[/color]
        My client wants me to build a sort method for the results. ofcourse it has
        to run before the paging one, so the paging will be right.

        So what i need help in, is building that Sort method, which will work on ANY
        of the currect discount property's.

        I hope my question is easier to understand now.

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: A better explanation of what i need help in

          eLisHa <eLisHa@discuss ions.microsoft. com> wrote:[color=blue]
          > So what i need help in, is building that Sort method, which will work on ANY
          > of the currect discount property's.[/color]

          Well, it would probably be best to have a separate implementation of
          IComparer for each property - e.g. DiscountIDCompa rer,
          DiscountNameCom parer etc. Implement IComparer, create an instance of
          the implementation, then pass that to ArrayList.Sort.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • eLisHa

            #6
            Re: A better explanation of what i need help in

            Thank you for replying... i was wondering, could you write a small example
            for me on how to create this sort method related to the new IComparer which i
            need to build for each property (could use help there too ;))

            Again, Thank you!

            "Jon Skeet [C# MVP]" wrote:
            [color=blue]
            > eLisHa <eLisHa@discuss ions.microsoft. com> wrote:[color=green]
            > > So what i need help in, is building that Sort method, which will work on ANY
            > > of the currect discount property's.[/color]
            >
            > Well, it would probably be best to have a separate implementation of
            > IComparer for each property - e.g. DiscountIDCompa rer,
            > DiscountNameCom parer etc. Implement IComparer, create an instance of
            > the implementation, then pass that to ArrayList.Sort.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too
            >[/color]

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: A better explanation of what i need help in

              eLisHa <eLisHa@discuss ions.microsoft. com> wrote:[color=blue]
              > Thank you for replying... i was wondering, could you write a small example
              > for me on how to create this sort method related to the new IComparer which i
              > need to build for each property (could use help there too ;))[/color]

              You don't need to create the sort method. Suppose you have a variable
              "list" which is an ArrayList. You'd just do:

              IComparer foo = new DiscountIDCompa rer(); // For instance
              list.Sort(foo);

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • eLisHa

                #8
                Re: A better explanation of what i need help in

                Hmm, i think i got it.... except the

                IComparer foo = new DiscountIDCompa rer()

                What is DiscountIDCompa rer? I need to create it for each property? What type
                / value it has?

                Thanks again.

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: A better explanation of what i need help in

                  eLisHa <eLisHa@discuss ions.microsoft. com> wrote:[color=blue]
                  > Hmm, i think i got it.... except the
                  >
                  > IComparer foo = new DiscountIDCompa rer()
                  >
                  > What is DiscountIDCompa rer? I need to create it for each property? What type
                  > / value it has?[/color]

                  As I said before, it's a type which implements IComparer. It would
                  compare two any discounts by ID. You'd have a different one for each
                  property.

                  You *could* do it with one type, either with a big switch statement
                  (nasty) or reflection (not type safe, relatively slow) but IMO the
                  simplest thing would be to have a separate type for each property you
                  need to sort by.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • eLisHa

                    #10
                    Re: A better explanation of what i need help in

                    Thanks again for replying... but i still dont get the point.
                    I have this class - Discount, which exists in my ArrayList dynamic times.
                    I want to sort the ArrayList, so i understood from you that i need to create
                    a IComparer to do that, an IComparer for each field to sort by.

                    Now - i dont understand how to do that - creating the IComparer for sorting
                    by.
                    I looked in .net sdk, and IComparer doesnt have a constructor (v1.1). It
                    just has one member, a method - Compare.

                    How do i create a new instance of it (for instance - DiscountIDCompa rer),
                    that will know to compare the DiscountID's?

                    Thank you so much.

                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: A better explanation of what i need help in

                      eLisHa <eLisHa@discuss ions.microsoft. com> wrote:[color=blue]
                      > Thanks again for replying... but i still dont get the point.
                      > I have this class - Discount, which exists in my ArrayList dynamic times.
                      > I want to sort the ArrayList, so i understood from you that i need to create
                      > a IComparer to do that, an IComparer for each field to sort by.[/color]

                      Yes.
                      [color=blue]
                      > Now - i dont understand how to do that - creating the IComparer for sorting
                      > by.
                      > I looked in .net sdk, and IComparer doesnt have a constructor (v1.1). It
                      > just has one member, a method - Compare.
                      >
                      > How do i create a new instance of it (for instance - DiscountIDCompa rer),
                      > that will know to compare the DiscountID's?[/color]

                      It's an interface. You need to implement the interface yourself:

                      public class DiscountIDCompa rer : IComparer
                      {
                      public int Compare (object first, object second)
                      {
                      // Compare first and second - they should both
                      // be references to Discount instances.
                      // See the docs for more information
                      }
                      }

                      If you haven't used interfaces before, now would be a very good time to
                      put your project down temporarily and learn about them before you
                      continue. They're very, very important.

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      Working...