Sort an Array List

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

    #1

    Sort an Array List

    Hi,
    I have a function that loads Rows from a field in a DataTable into an
    ArrayList.
    See below:

    It also adds a Row (eg "All Areas").
    I wanted this new row to be the first record so I added a preceeding space
    (eg " All Areas") and sorted the ArrayList.

    I have just found that there are some valid records that have one or more
    spaces so my added record is no longer first item in the list.

    Is there a way to create a custom sort for the ArrayList so that my added
    item is first in the list?

    Thanks

    Doug





    Private Function dacFillCombo(By Val stTblNam As String, ByVal stDspFld As
    String, _

    ByVal stDsplVal As String) As ArrayList

    Dim dr As DataRow, al As New ArrayList

    Try

    al.Add(stDsplVa l)

    For Each dr In dsFiles.Tables( stTblNam).Rows

    al.Add(dr(stDsp Fld).ToString)

    Next

    al.Sort()

    Return al

    Catch ex As Exception

    MsgBox(ex.ToStr ing)

    End Try

    End Function


  • Cor Ligthert [MVP]

    #2
    Re: Sort an Array List

    Doug,

    What do you want to achieve?

    It looks to something that can be done in one or more methods/properties in
    either the datatable or either the Combobox.

    Cor


    Comment

    • Doug Bell

      #3
      Re: Sort an Array List

      Hi Cor,
      What I am trying to achieve is to have an array list (independant of the
      DataTable)
      sorted ascending but with one item that is added to the list (and it does
      not come from the datatable) always set to be the first item.

      The array list is the data source for one or more combo boxes.

      I created a function to fill the populate the list and add the new item and
      set it to the combo box because this happens with a number of combo boxes
      and with a number of tables.

      Doug

      "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
      news:uuj1KJqsFH A.1028@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Doug,
      >
      > What do you want to achieve?
      >
      > It looks to something that can be done in one or more methods/properties[/color]
      in[color=blue]
      > either the datatable or either the Combobox.
      >
      > Cor
      >
      >[/color]


      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Sort an Array List

        Dough,

        I don't see the purpose of the arraylist while you can create as much
        dataviews as you want which do the job probably much easier, so why an
        arraylist?

        Cor


        Comment

        • Doug Bell

          #5
          Re: Sort an Array List

          How do I add an extra item to a dataview?

          "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
          news:ufWnCgqsFH A.1028@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Dough,
          >
          > I don't see the purpose of the arraylist while you can create as much
          > dataviews as you want which do the job probably much easier, so why an
          > arraylist?
          >
          > Cor
          >
          >[/color]


          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Sort an Array List

            Doug,
            [color=blue]
            > How do I add an extra item to a dataview?
            >[/color]
            If you only want to proof that your arraylist is the solution without
            telling why, than don't ask for help.

            Probably there are better solutions and therefore it has no reason for
            others to spent time for that.

            In my opinion should you than just investigate it yourself.

            Cor


            Comment

            • Doug Bell

              #7
              Re: Sort an Array List

              Cor,
              I don't understand your response.

              As I explained, I need to add a row item and then sort the data except for
              the new item that has to be at the top of the list.

              You have not provided me with a solution except to say that a DataView is
              better than using an Array List.

              If you can explain how I can add an item to a DataView and then do a sort so
              that everythin is sorted ascending except for the new item which should be
              the top item, then I will appreciate your response.

              If you can not provide a solution then that is fine also. (I realise MVPs
              can only do so much and it is good that they give up time to help others)

              Using an ArrayList allows the new item to be added but I don't know if there
              is a way to sort it to get the desired result, just as I don't know if there
              is a way to sort a dataview to get that result.

              Doug


              "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
              news:egnU0MrsFH A.1328@TK2MSFTN GP10.phx.gbl...[color=blue]
              > Doug,
              >[color=green]
              > > How do I add an extra item to a dataview?
              > >[/color]
              > If you only want to proof that your arraylist is the solution without
              > telling why, than don't ask for help.
              >
              > Probably there are better solutions and therefore it has no reason for
              > others to spent time for that.
              >
              > In my opinion should you than just investigate it yourself.
              >
              > Cor
              >
              >[/color]


              Comment

              • Cor Ligthert [MVP]

                #8
                Re: Sort an Array List

                Doug.

                With the few information you gave me, will adding a column in your datatable
                probably be the most easy solution. That column can be than the source for
                your sortproperty in the dataview.

                I hope this helps,

                Cor


                Comment

                • Doug Bell

                  #9
                  Re: Sort an Array List

                  Thanks Cor,

                  That does give me something to work on.

                  I can't add a column to the DataTable as the new item is not in the
                  datatable but I can add a column to the ArrayList and maybe sort by column
                  and then by the column that holds the data (assuming that I can sort the
                  list by 2 columns).

                  I can not change the datatable/s as they are common sources for many
                  different selection combo boxes.

                  Doug
                  "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                  news:OUh8fzrsFH A.996@TK2MSFTNG P14.phx.gbl...[color=blue]
                  > Doug.
                  >
                  > With the few information you gave me, will adding a column in your[/color]
                  datatable[color=blue]
                  > probably be the most easy solution. That column can be than the source for
                  > your sortproperty in the dataview.
                  >
                  > I hope this helps,
                  >
                  > Cor
                  >
                  >[/color]


                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: Sort an Array List

                    Doug,
                    [color=blue]
                    > I can't add a column to the DataTable as the new item is not in the
                    > datatable[/color]

                    Why not, if you cannot add it to the datatable, than it is probably more
                    work to add it to an arraylist?

                    Cor


                    Comment

                    • Ken Tucker [MVP]

                      #11
                      Re: Sort an Array List

                      Hi,

                      Take a look at the sortedlist class.

                      http://msdn.microsoft.com/library/de...classtopic.asp

                      Ken
                      ---------------------
                      "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                      news:uuj1KJqsFH A.1028@TK2MSFTN GP12.phx.gbl...[color=blue]
                      > Doug,
                      >
                      > What do you want to achieve?
                      >
                      > It looks to something that can be done in one or more methods/properties
                      > in either the datatable or either the Combobox.
                      >
                      > Cor
                      >[/color]


                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: Sort an Array List

                        Doug,
                        In addition to using a SortedList, you can create a custom Comparer object
                        (an object that implements IComparer) & pass it to the overloaded
                        ArrayList.Sort method.

                        http://msdn.microsoft.com/library/de...sorttopic2.asp

                        Something like:

                        Public Class AreaComparer
                        Implements IComparer

                        Public Function Compare(ByVal x As Object, ByVal y As Object) As
                        Integer Implements System.Collecti ons.IComparer.C ompare
                        Dim row1 As DataRow = DirectCast(x, DataRow)
                        Dim row2 As DataRow = DirectCast(y, DataRow)

                        If String.Equals(r ow1!name, "All Areas") AndAlso
                        String.Equals(r ow2!name, "All Areas") Then
                        Return 0
                        ElseIf String.Equals(r ow1!name, "All Areas") Then
                        Return -1
                        ElseIf String.Equals(r ow2!name, "All Areas") Then
                        Return 1
                        Else
                        Return Comparer.Defaul tInvariant.Comp are(row1!name,
                        row2!name)
                        End If
                        End Function

                        End Class

                        Public Shared Sub Main()
                        Dim table As New DataTable("area ")
                        table.Columns.A dd("name", GetType(String) )
                        table.Rows.Add( New Object() {"area 2"})
                        table.Rows.Add( New Object() {"area 1"})
                        table.Rows.Add( New Object() {"area 4"})
                        table.Rows.Add( New Object() {"area 3"})
                        table.Rows.Add( New Object() {" rea 5"})
                        Dim list As New ArrayList

                        list.AddRange(t able.Rows)

                        Dim row As DataRow = table.NewRow()
                        row!name = "All Areas"
                        list.Add(row)
                        list.Sort(New AreaComparer)
                        For Each row In list
                        Debug.WriteLine (row!name, "area")
                        Next

                        End Sub

                        The above assumes there is only one row named "All Areas". AreaComparer
                        could be modified with a constructor that defines the value to look for &
                        column to look for (in which case I would use Object.Equals instead of
                        String.Equals so its "fully polymorphic".

                        Hope this helps
                        Jay

                        "Doug Bell" <PoorSupport@vo daphone.com.au> wrote in message
                        news:%23$ldXKos FHA.3180@TK2MSF TNGP10.phx.gbl. ..
                        | Hi,
                        | I have a function that loads Rows from a field in a DataTable into an
                        | ArrayList.
                        | See below:
                        |
                        | It also adds a Row (eg "All Areas").
                        | I wanted this new row to be the first record so I added a preceeding space
                        | (eg " All Areas") and sorted the ArrayList.
                        |
                        | I have just found that there are some valid records that have one or more
                        | spaces so my added record is no longer first item in the list.
                        |
                        | Is there a way to create a custom sort for the ArrayList so that my added
                        | item is first in the list?
                        |
                        | Thanks
                        |
                        | Doug
                        |
                        |
                        |
                        |
                        |
                        | Private Function dacFillCombo(By Val stTblNam As String, ByVal stDspFld As
                        | String, _
                        |
                        | ByVal stDsplVal As String) As ArrayList
                        |
                        | Dim dr As DataRow, al As New ArrayList
                        |
                        | Try
                        |
                        | al.Add(stDsplVa l)
                        |
                        | For Each dr In dsFiles.Tables( stTblNam).Rows
                        |
                        | al.Add(dr(stDsp Fld).ToString)
                        |
                        | Next
                        |
                        | al.Sort()
                        |
                        | Return al
                        |
                        | Catch ex As Exception
                        |
                        | MsgBox(ex.ToStr ing)
                        |
                        | End Try
                        |
                        | End Function
                        |
                        |


                        Comment

                        • Cor Ligthert [MVP]

                          #13
                          Re: Sort an Array List

                          Jay,

                          Can you enlighten me what is the advance of your solution above what I did
                          suggest to add a column to the datatable. In this particulary case because
                          an expression is probably not possible to itterate it and add that as the
                          sort sequence and than to use a dataview to set the combobox?

                          Cor


                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: Sort an Array List

                            Cor,
                            There is none per se. They are both alternative ends to a means.

                            The way I read Doug's message: Doug wants to add a "special" Row to the
                            "bound DataTable", not a new Column per se.

                            I can see simply adding the "special" row to the DataTable itself & bind to
                            the DataTable/DataView. I can see using a DataView's Filter to
                            include/exclude this "special" row, from individual bindings of the data. I
                            can see including a column in the DataTable to indicate it is a normal row
                            or a special row (simplifying the aforementioned filter). The "disadvanta ge"
                            of adding a "special" row, is that it a DataAdapter might attempt to update
                            the database with it.

                            The "advantage" of using the ArrayList or SortedList is that a copy of the
                            collection is made (not the data per se), thus there is no chance of messing
                            up your DataAdapters. It would not be my first choice, however it was
                            something Doug sounded comfortable with.

                            With effort I could see creating a special DataView that automatically
                            inserted a "row 0" that represented the "special row", delegating all the
                            other methods to an underlying DataView. In a lot of ways I like the special
                            DataView idea "the best". However I think it would be more work to get it
                            working correctly... Especially considering you would need to return a
                            DataRowView w/DataRow... Then again creating a special DataView might not be
                            a viable solution...

                            Hope this helps
                            Jay






                            "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                            news:%23vwxnG$s FHA.1704@TK2MSF TNGP11.phx.gbl. ..
                            | Jay,
                            |
                            | Can you enlighten me what is the advance of your solution above what I did
                            | suggest to add a column to the datatable. In this particulary case because
                            | an expression is probably not possible to itterate it and add that as the
                            | sort sequence and than to use a dataview to set the combobox?
                            |
                            | Cor
                            |
                            |


                            Comment

                            • Cor Ligthert [MVP]

                              #15
                              Re: Sort an Array List

                              Jay,
                              [color=blue]
                              >The "disadvanta ge"
                              > of adding a "special" row, is that it a DataAdapter might attempt to
                              > update
                              > the database with it.
                              >[/color]

                              In my opinion are we not talking about a special row. It is about the row
                              that is the latest added new, which has to be the first in the combobox. In
                              other words the "dataviewso rt field "where the index is equal to the
                              rowcount has to have the lowest value.

                              Cor


                              Comment

                              Working...