Sort an Array List

D

Doug Bell

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(ByVal stTblNam As String, ByVal stDspFld As
String, _

ByVal stDsplVal As String) As ArrayList

Dim dr As DataRow, al As New ArrayList

Try

al.Add(stDsplVal)

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

al.Add(dr(stDspFld).ToString)

Next

al.Sort()

Return al

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Function
 
C

Cor Ligthert [MVP]

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
 
D

Doug Bell

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
 
C

Cor Ligthert [MVP]

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
 
C

Cor Ligthert [MVP]

Doug,
How do I add an extra item to a dataview?
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
 
D

Doug Bell

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
 
C

Cor Ligthert [MVP]

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
 
D

Doug Bell

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
 
C

Cor Ligthert [MVP]

Doug,
I can't add a column to the DataTable as the new item is not in the
datatable

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

Cor
 
J

Jay B. Harlow [MVP - Outlook]

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/d...systemcollectionsarraylistclasssorttopic2.asp

Something like:

Public Class AreaComparer
Implements IComparer

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

If String.Equals(row1!name, "All Areas") AndAlso
String.Equals(row2!name, "All Areas") Then
Return 0
ElseIf String.Equals(row1!name, "All Areas") Then
Return -1
ElseIf String.Equals(row2!name, "All Areas") Then
Return 1
Else
Return Comparer.DefaultInvariant.Compare(row1!name,
row2!name)
End If
End Function

End Class

Public Shared Sub Main()
Dim table As New DataTable("area")
table.Columns.Add("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(table.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

| 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(ByVal stTblNam As String, ByVal stDspFld As
| String, _
|
| ByVal stDsplVal As String) As ArrayList
|
| Dim dr As DataRow, al As New ArrayList
|
| Try
|
| al.Add(stDsplVal)
|
| For Each dr In dsFiles.Tables(stTblNam).Rows
|
| al.Add(dr(stDspFld).ToString)
|
| Next
|
| al.Sort()
|
| Return al
|
| Catch ex As Exception
|
| MsgBox(ex.ToString)
|
| End Try
|
| End Function
|
|
 
C

Cor Ligthert [MVP]

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
 
J

Jay B. Harlow [MVP - Outlook]

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 "disadvantage"
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






| 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
|
|
 
C

Cor Ligthert [MVP]

Jay,
The "disadvantage"
of adding a "special" row, is that it a DataAdapter might attempt to
update
the database with it.

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 "dataviewsort field "where the index is equal to the
rowcount has to have the lowest value.

Cor
 
C

Cor Ligthert [MVP]

It should not be needed however to clear eventually misunderstandings.
where the index is equal to the rowcount has to have the lowest value.

where the index of the *datatable* is equal to the
................................
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
The way I read Doug's original messages:

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

Is that he has a list of Areas, and then he wants an "All Areas" as the
first item in the list. Ergo he wants a special row that indicates all
items, given a list of specific items. Of course either or both of us may be
reading Doug's message incorrectly.

In ASP.NET apps I will have a "special row" that requests the user select an
item, however I normally add the item after I use the DataBind command.
Unfortunately for Windows Forms apps its not that easy...

Just a thought
Jay


| Jay,
|
| >The "disadvantage"
| > of adding a "special" row, is that it a DataAdapter might attempt to
| > update
| > the database with it.
| >
|
| 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 "dataviewsort field "where the index is equal to the
| rowcount has to have the lowest value.
|
| Cor
|
|
 
C

Cor Ligthert [MVP]

Jay,

I agree with you, if it is just a kind of non changing header row, than it
can be as you said.

Although *I* than probably would just create a filled clone datatable with
an extra row. However that is than probably only my preference for
datatables in relation to datagrids (datagridviews).

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Cor,
I like the cloned datatable idea. That's a good alternative depending on
what Doug (& others) are trying to achieve.

Jay

| Jay,
|
| I agree with you, if it is just a kind of non changing header row, than it
| can be as you said.
|
| Although *I* than probably would just create a filled clone datatable with
| an extra row. However that is than probably only my preference for
| datatables in relation to datagrids (datagridviews).
|
| Cor
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top