473,698 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with sorting an Arraylist that contains Structures

Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs from
it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure.Ta bName?

I would greatly appreciate it.

Aug 21 '08 #1
12 9757
Justin,

One option is to have your structure implement the IComparable interface:

Structure TabStructure
Implements IComparable(Of TabStructure)
Dim TabName As String
Dim FullFilePath As String

Function CompareTo(ByVal other As TabStructure) As Integer
Implements IComparable(Of TabStructure).C ompareTo

Return Me.TabName.Comp areTo(other.Tab Name)

End Function

End Structure

Now ArrayList.Sort should sort the "tabs" by TabName.

By the way, I would use a class instead of a structure.

Kerry Moorman

"Justin" wrote:
Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs from
it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure.Ta bName?

I would greatly appreciate it.

Aug 22 '08 #2
Hi Justin,

First, why are you using ArrayList ? You should use the generic List, eg.

Dim myList as New List(Of TabStructure)

Then, assuming you are using VB 2008, you can pass in a lambda function to
the sort method:

myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )



"Justin" <No**@None.comw rote in message
news:OE******** ********@TK2MSF TNGP03.phx.gbl. ..
Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs from
it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure.Ta bName?

I would greatly appreciate it.
Aug 22 '08 #3
First, until just now I never heard of "lists". However you mention VS2008
specifically and while I am using VS2008 I'm wanting to stay with in the
confines of framework 2. Does your suggestion do that? If so then I'll
read up on it.

Googling "VB.NET list" came up with nothing but arraylists and listboxes.

I already know arraylists and I already know how to check for dups before
adding so it was pretty simple to bang out the code. This arraylist is used
all over my app.

"Bill McCarthy" <Bi**@localhost .comwrote in message
news:12******** *************** ***********@mic rosoft.com...
Hi Justin,

First, why are you using ArrayList ? You should use the generic List, eg.

Dim myList as New List(Of TabStructure)

Then, assuming you are using VB 2008, you can pass in a lambda function to
the sort method:

myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )



"Justin" <No**@None.comw rote in message
news:OE******** ********@TK2MSF TNGP03.phx.gbl. ..
>Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs from
it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure.T abName?

I would greatly appreciate it.
Aug 22 '08 #4
Eh, this is my only instance and it was just 4 lines of code. I was
wondering how to implement IComparable. Now I get it. This make much more
sense then what I was reading earlier today.

Thanks! I'll play with this tomorrow.
"Kerry Moorman" <Ke**********@d iscussions.micr osoft.comwrote in message
news:36******** *************** ***********@mic rosoft.com...
Justin,

One option is to have your structure implement the IComparable interface:

Structure TabStructure
Implements IComparable(Of TabStructure)
Dim TabName As String
Dim FullFilePath As String

Function CompareTo(ByVal other As TabStructure) As Integer
Implements IComparable(Of TabStructure).C ompareTo

Return Me.TabName.Comp areTo(other.Tab Name)

End Function

End Structure

Now ArrayList.Sort should sort the "tabs" by TabName.

By the way, I would use a class instead of a structure.

Kerry Moorman

"Justin" wrote:
>Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs from
it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure.T abName?

I would greatly appreciate it.

Aug 22 '08 #5
The generic List(Of T) is in 2.0. Its advantage over ArrayList is
that the contents of the list is typed so you don't have to cast the
contents and therefore avoid runtime errors if you make a mistake
casting the contents. I also strongly urge you to use it instead of
ArrayList.

On Thu, 21 Aug 2008 20:48:26 -0700, "Justin" <No**@None.comw rote:
>First, until just now I never heard of "lists". However you mention VS2008
specifically and while I am using VS2008 I'm wanting to stay with in the
confines of framework 2. Does your suggestion do that? If so then I'll
read up on it.

Googling "VB.NET list" came up with nothing but arraylists and listboxes.

I already know arraylists and I already know how to check for dups before
adding so it was pretty simple to bang out the code. This arraylist is used
all over my app.

"Bill McCarthy" <Bi**@localhost .comwrote in message
news:12******* *************** ************@mi crosoft.com...
>Hi Justin,

First, why are you using ArrayList ? You should use the generic List, eg.

Dim myList as New List(Of TabStructure)

Then, assuming you are using VB 2008, you can pass in a lambda function to
the sort method:

myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )



"Justin" <No**@None.comw rote in message
news:OE******* *********@TK2MS FTNGP03.phx.gbl ...
>>Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs from
it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure. TabName?

I would greatly appreciate it.
Aug 22 '08 #6
Cool, I'll check it out. Thanks.
"Jack Jackson" <jj******@cinno vations.netwrot e in message
news:c0******** *************** *********@4ax.c om...
The generic List(Of T) is in 2.0. Its advantage over ArrayList is
that the contents of the list is typed so you don't have to cast the
contents and therefore avoid runtime errors if you make a mistake
casting the contents. I also strongly urge you to use it instead of
ArrayList.

On Thu, 21 Aug 2008 20:48:26 -0700, "Justin" <No**@None.comw rote:
>>First, until just now I never heard of "lists". However you mention
VS2008
specificall y and while I am using VS2008 I'm wanting to stay with in the
confines of framework 2. Does your suggestion do that? If so then I'll
read up on it.

Googling "VB.NET list" came up with nothing but arraylists and listboxes.

I already know arraylists and I already know how to check for dups before
adding so it was pretty simple to bang out the code. This arraylist is
used
all over my app.

"Bill McCarthy" <Bi**@localhost .comwrote in message
news:12****** *************** *************@m icrosoft.com...
>>Hi Justin,

First, why are you using ArrayList ? You should use the generic List,
eg.

Dim myList as New List(Of TabStructure)

Then, assuming you are using VB 2008, you can pass in a lambda function
to
the sort method:

myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )



"Justin" <No**@None.comw rote in message
news:OE****** **********@TK2M SFTNGP03.phx.gb l...
Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs
from
it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure .TabName?

I would greatly appreciate it.
Aug 22 '08 #7
Hi Justin,

List(Of T) basically replaces ArrayList as of .NET 2.0//VB 2005. It uses
generics, so the item type is strongly typed. With value types, such as your
structure, this avoids costly boxing and unboxing the arraylist has to
perform. So you get more robust code and in many cases a bit of a
performance improvement too.

As to compiling against the 2.0 framework, in VB 2008 in project options,
compile tab, select Advanced Compile Options and set the target framework to
2.0, 3.0 or 3.5. If you are using features not supported by the target
framework, VB will warn you. The example of the lambda function I gave
should compile to 2.0 without problem. The lambda function, is basically an
inline function.

As to generics, it's been a while since I wrote much about them, but perhaps
this old article may help introduce you to them:
http://visualstudiomagazine.com/feat...torialsid=1731

"Justin" <No**@None.comw rote in message
news:O$******** ******@TK2MSFTN GP03.phx.gbl...
First, until just now I never heard of "lists". However you mention
VS2008 specifically and while I am using VS2008 I'm wanting to stay with
in the confines of framework 2. Does your suggestion do that? If so then
I'll read up on it.

Googling "VB.NET list" came up with nothing but arraylists and listboxes.

I already know arraylists and I already know how to check for dups before
adding so it was pretty simple to bang out the code. This arraylist is
used all over my app.

"Bill McCarthy" <Bi**@localhost .comwrote in message
news:12******** *************** ***********@mic rosoft.com...
>Hi Justin,

First, why are you using ArrayList ? You should use the generic List,
eg.

Dim myList as New List(Of TabStructure)

Then, assuming you are using VB 2008, you can pass in a lambda function
to the sort method:

myList.Sort(Fun ction(x, y) x.TabName.Compa reTo(y.TabName) )



"Justin" <No**@None.comw rote in message
news:OE******* *********@TK2MS FTNGP03.phx.gbl ...
>>Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs
from it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure. TabName?

I would greatly appreciate it.
Aug 22 '08 #8
On 2008-08-21, Justin <No**@None.comw rote:
Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs from
it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure.Ta bName?

I would greatly appreciate it.
Others have point out to you the List(Of T) - so I won't rehash that :) But,
I'm going to talk about your structure. The fact is, there is almost never
any reason to use a structure except for interop scenarios or where you want
to preserve value symantics... TabStructure should be a class. This will
become apparent as soon as you start trying to modify individual members of
your list:

Private Structure TStruct
Public s1 As String
Public s2 As String

Public Sub New(ByVal s1 As String, ByVal s2 As String)
Me.s1 = s1
Me.s2 = s2
End Sub
End Structure

Sub Main()
Dim l As New List(Of TStruct)
l.Add(New TStruct("one", "one"))
l.Add(New TStruct("two", "one"))
l.Add(New TStruct("three" , "one"))
l.Add(New TStruct("four", "one"))

For Each i As TStruct In l
i.s2 = "changing.. ."
Next

For Each i As TStruct In l
Console.WriteLi ne("s1={0}, s2={1}", i.s1, i.s2)
Next
End Sub

You might be suprised that the values of s2 changend in the first loop, don't
actually change anything. That's because TStruct is a value type, and any
time you assign or return it you get a copy and not the original. If I change
TStruct to a class:

Private Class TStruct
Public s1 As String
Public s2 As String

Public Sub New(ByVal s1 As String, ByVal s2 As String)
Me.s1 = s1
Me.s2 = s2
End Sub
End Class

Then every thing works as expected.

So, again unless your doing interop or really want value symantics, there is
not much gained by using a struct over a class.

--
Tom Shelton
Aug 22 '08 #9
Ok, after getting beat in the head with "class" by 4 people I converted it
to a class :)

I tried to convert my arraylist to a list and that was just an ugly mess.
Here's the first two errors I received:
'TabsArray' cannot expose type 'TabClass' outside the project through class
'frmMain'
Too few type arguments to 'System.Collect ions.Generic.Li st(Of T)'

But enough of that. I want to wrap my head around iComparable first. I'll
deal with that later.

I implemented iComparable and now when I try to .sort my arraylist I get:

"Failed to compare two elements in the array."

Again, thanks for the help everyone!


"Kerry Moorman" <Ke**********@d iscussions.micr osoft.comwrote in message
news:36******** *************** ***********@mic rosoft.com...
Justin,

One option is to have your structure implement the IComparable interface:

Structure TabStructure
Implements IComparable(Of TabStructure)
Dim TabName As String
Dim FullFilePath As String

Function CompareTo(ByVal other As TabStructure) As Integer
Implements IComparable(Of TabStructure).C ompareTo

Return Me.TabName.Comp areTo(other.Tab Name)

End Function

End Structure

Now ArrayList.Sort should sort the "tabs" by TabName.

By the way, I would use a class instead of a structure.

Kerry Moorman

"Justin" wrote:
>Ok, I give up. I can't seem to construct a decent (productive) way of
sorting my arraylist.

I have a structure of two elements:

Structure TabStructure
Dim TabName As String
Dim FullFilePath As String
End Structure

Then for example I:

Dim tab as New TabStructure

some code

Then I add "tab" to an arraylist.

At some point I loop through my arraylist, grab data and create tabs from
it. Of course the resulting list of tabs are out of sequence.

Can someone help me to sort my array list alphabetically by
TabStructure.T abName?

I would greatly appreciate it.

Aug 22 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2524
by: Herman | last post by:
Hi everyone, I'm implementing Dijkstra's algorithm for a class project, and I'm having trouble compiling the class that runs the algorithm. All of the other files compile fine except for this one. I am using the compiler like this: javac -classpath .. <filename.java> The source code of the classes follow. Dijkstra.java is the class that won't compile, because the compiler doesn't recgonise the Vertex and GraphMap classes, even...
4
1828
by: jarkkotv | last post by:
Hi everyone! I'm having a little problem when sorting the ArrayList and I was wondering if there is a .NET guru who can help me out :) I'm trying to sort ArrayList alphabetically in ASP.Net -page using the ArrayList.Sort(), but I not getting the results sorted properly. I'm having problems especially with the scandic characters although CurrentCulture is set to Finnish, which should handle these charcters
2
3585
by: Rustam Asgarov | last post by:
Hi. I have ArrayList that contains structures... I need to iterate through all structures in this array and update one of members in this structures... The first thing that comes to mind is just use foreach, store struct in temp variable , update it , then write it back to array to the same position. Is there more efficient way? Thanks..
1
1737
by: Joe | last post by:
Hello All: I am writing a function which accepts an ArrayList as its parameter and converts the contents of the ArrayList into an XmlNodeList. The ArrayList will contain one of several different structures. For example, the structures could be either a Customer structure (which contains a customer id and name) or an Address structure (which contains an address type and street number, city, etc). The XmlNodeList is uded to populate a...
0
1906
by: Brian Henry | last post by:
Ok I've never implemented a snap location before so I dont really know what im doing wrong here... anyways, I am making a custom slider control that takes dates as its values instead of integers... then taking that date range and finding dates specifiec between them (in a list of dates) and putting snap marks, so if you slide it near one of them it should snap to that tick, but that part i cant figure out. the rest seems ok so far... here...
3
2627
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: code:------------------------------------------------------------------------------ class Program { static public ArrayList MyArrayList = new ArrayList();
19
4380
by: NycCraze88 | last post by:
Hey all, I'm in a Java data structures class in college and I have a problem with this one sorting assignment. I'm not very experienced in java and have tried tackling this assignment in an assortment of ways but have failed miserably. Basically, here is the assignment::: ======================================================== "If you look in http://csis.pace.edu/~bergin/temp/circles.zip , you will find a file containing,...
1
3267
by: =?Utf-8?B?SmF5Qw==?= | last post by:
I am trying to understand how to use an arraylist that contains data in a structure and bind the results to a gridview. Using vs2008 I have looked at the examples 315784 HOW TO: Bind a DataGrid Control to an Array of Objects or Structures by http://support.microsoft.com/?id=315784 316302 HOW TO: Bind a DataGrid Control to an ArrayList of Objects or Structures http://support.microsoft.com/?id=316302
2
10032
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.