473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 1732
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brett | last post by:
I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array. ie. @l1={3,1,2};...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
9
by: gl | last post by:
How do I take an array or arraylist, and sort it randomly? Like suppose the items in it are (1,2,3,4,5) and I want to get it to be in a random order (2,3,1,4,5). How do you do that? I think it's a...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
6
by: Nick Valeontis | last post by:
I know how to use Icomparable. However, I can't figure out how to sort a generic linked list? (without writing the algorithm) Lets say I have something like this: class...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
0
Ganon11
by: Ganon11 | last post by:
Earlier, mmccarthy was kind enough to post a short article explaining the Bubble Sort. As she said, this is a relatively slow sorting algorithm. Here, I will attempt to present a slightly faster...
4
by: artev | last post by:
if I have one simple array 1 4 2 6 for to order the code is: arrayOrdin=arraySimple.sort(); and I have 1
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.