469,576 Members | 1,684 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,576 developers. It's quick & easy.

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(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
Nov 21 '05 #1
18 1560
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
Nov 21 '05 #2
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]" <no************@planet.nl> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #3
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
Nov 21 '05 #4
How do I add an extra item to a dataview?

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:uf**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #5
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
Nov 21 '05 #6
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]" <no************@planet.nl> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #7
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
Nov 21 '05 #8
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]" <no************@planet.nl> wrote in message
news:OU*************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #9
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
Nov 21 '05 #10
Hi,

Take a look at the sortedlist class.

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

Ken
---------------------
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #11
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.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

"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:%2****************@TK2MSFTNGP10.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(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
|
|
Nov 21 '05 #12
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
Nov 21 '05 #13
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


"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP11.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
|
|
Nov 21 '05 #14
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
Nov 21 '05 #15
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
................................
Nov 21 '05 #16
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
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
| 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
|
|
Nov 21 '05 #17
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
Nov 21 '05 #18
Cor,
I like the cloned datatable idea. That's a good alternative depending on
what Doug (& others) are trying to achieve.

Jay

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:eg*************@TK2MSFTNGP12.phx.gbl...
| 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
|
|
Nov 21 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Brett | last post: by
3 posts views Thread by chellappa | last post: by
9 posts views Thread by gl | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
6 posts views Thread by Nick Valeontis | last post: by
reply views Thread by JosAH | last post: by
Ganon11
reply views Thread by Ganon11 | last post: by
4 posts views Thread by artev | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.