473,287 Members | 1,501 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,287 software developers and data experts.

Array.sort question

Let's say I have a class of people...

Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class

And I declare an array of this class...

Dim MyPeopleArray(3) as People

Then I fill my arry with 3 people...

MyPeopleArray(0).Sex = "Female"
MyPeopleArray(0).Age = 32
MyPeopleArray(0).Name = "Marsha Marsha Marsha"

MyPeopleArray(1).Sex = "Male"
MyPeopleArray(1).Age = 31
MyPeopleArray(1).Name = "John"

MyPeopleArray(2).Sex = "Can't decide"
MyPeopleArray(2).Age = "23"
MyPeopleArray(2).Name = "Don't lable me"

How would I resort my the MyPeopleArray to have the people in Age order,
name order or sex order?

Thanks!

Jim

Jul 19 '05 #1
4 3664
Jim,
Define a PeopleComparer class that implements the IComparer interface. The
PeopleComparer object would know which property you want to sort on and act
accordingly.

When you call Array.Sort, you would use the overloaded version that accepts
an IComparer.

For an example see:
http://msdn.microsoft.com/library/de...SortTopic3.asp

I would define the PeopleComparer to accept the property name as a parameter
to the constructor then have a select case in the compare.

Something like:

Public Class PeopleComparer
Implements IComparer

Private Readonly m_field As String

Public Sub New()
MyClass.New("Name")
End Sub

Public Sub New(ByVal field As String)
m_field = field
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer _
Implements IComparer.Compare
Dim xo As People = DirectCast(x, People)
Dim yo As People = DirectCast(y, People)
Select Case m_field
Case "Sex"
Return xo.Sex.CompareTo(yo.Sex)
Case "Age"
Return xo.Age.CompareTo(yo.Age)
Case "Name"
Return xo.Name.CompareTo(yo.Name)
End Select
End Function

End Class
Array.Sort(MyPeopleArray, New PeopleComparer("Age"))

Hope this helps
Jay

"its me" <at**@house.com> wrote in message
news:4s*****************@fe02.atl2.webusenet.com.. .
Let's say I have a class of people...

Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class

And I declare an array of this class...

Dim MyPeopleArray(3) as People

Then I fill my arry with 3 people...

MyPeopleArray(0).Sex = "Female"
MyPeopleArray(0).Age = 32
MyPeopleArray(0).Name = "Marsha Marsha Marsha"

MyPeopleArray(1).Sex = "Male"
MyPeopleArray(1).Age = 31
MyPeopleArray(1).Name = "John"

MyPeopleArray(2).Sex = "Can't decide"
MyPeopleArray(2).Age = "23"
MyPeopleArray(2).Name = "Don't lable me"

How would I resort my the MyPeopleArray to have the people in Age order,
name order or sex order?

Thanks!

Jim

Jul 19 '05 #2

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:ur**************@TK2MSFTNGP10.phx.gbl...
Jim,
Define a PeopleComparer class that implements the IComparer interface. The
PeopleComparer object would know which property you want to sort on and act accordingly.

When you call Array.Sort, you would use the overloaded version that accepts an IComparer.

For an example see:
http://msdn.microsoft.com/library/de...SortTopic3.asp
I would define the PeopleComparer to accept the property name as a parameter to the constructor then have a select case in the compare.

Something like:

Public Class PeopleComparer
Implements IComparer

Private Readonly m_field As String

Public Sub New()
MyClass.New("Name")
End Sub

Public Sub New(ByVal field As String)
m_field = field
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer _
Implements IComparer.Compare
Dim xo As People = DirectCast(x, People)
Dim yo As People = DirectCast(y, People)
Select Case m_field
Case "Sex"
Return xo.Sex.CompareTo(yo.Sex)
Case "Age"
Return xo.Age.CompareTo(yo.Age)
Case "Name"
Return xo.Name.CompareTo(yo.Name)
End Select
End Function

End Class
Array.Sort(MyPeopleArray, New PeopleComparer("Age"))

Hope this helps
Jay

"its me" <at**@house.com> wrote in message
news:4s*****************@fe02.atl2.webusenet.com.. .
Let's say I have a class of people...

Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class

And I declare an array of this class...

Dim MyPeopleArray(3) as People

Then I fill my arry with 3 people...

MyPeopleArray(0).Sex = "Female"
MyPeopleArray(0).Age = 32
MyPeopleArray(0).Name = "Marsha Marsha Marsha"

MyPeopleArray(1).Sex = "Male"
MyPeopleArray(1).Age = 31
MyPeopleArray(1).Name = "John"

MyPeopleArray(2).Sex = "Can't decide"
MyPeopleArray(2).Age = "23"
MyPeopleArray(2).Name = "Don't lable me"

How would I resort my the MyPeopleArray to have the people in Age order,
name order or sex order?

Thanks!

Jim



Thanks for the reply, but this solution does not emulate the Array.Sort
function. ICompare only compares two objects and tells you which comes
first. It does not sort the array and does not compare more than 2 objects.

I can code a solution, I was just making sure that I wasn't re-inventing the
wheel.

Thanks again.

Jim

Jul 19 '05 #3
Jim
Thanks for the reply, but this solution does not emulate the Array.Sort
function. ICompare only compares two objects and tells you which comes
first. It does not sort the array and does not compare more than 2 objects.
There is no need to 'emulate the Array.Sort' function, the Array.Sort
function does it for you! Please read the link I gave you for specifics.
http://msdn.microsoft.com/library/de...SortTopic3.asp

As I said you call the Array.Sort function that accepts a Comparer object.
You would pass it a PeopleComparer object, which I gave you, with the
correct property identified:

To sort the array in Age order:
Array.Sort(MyPeopleArray, New PeopleComparer("Age"))

To sort the array in Name order:
Array.Sort(MyPeopleArray, New PeopleComparer("Name"))

To sort the array in Sex order:
Array.Sort(MyPeopleArray, New PeopleComparer("Sex"))

The sort function then calls this Comparer object for each comparison it
needs to do, passing it one pair of objects. As the sort algorithm is
working it will repeatedly call the comparer object. Once all the objects
are in order, the sort function returns & presto your array is sorted.

Hope this helps
Jay
"Jim Hubbard" <Re*****@Group.net> wrote in message
news:yl***************@fe03.atl2.webusenet.com...
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:ur**************@TK2MSFTNGP10.phx.gbl...
Jim,
Define a PeopleComparer class that implements the IComparer interface. The PeopleComparer object would know which property you want to sort on and act
accordingly.

When you call Array.Sort, you would use the overloaded version that

accepts
an IComparer.

For an example see:

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

I would define the PeopleComparer to accept the property name as a

parameter
to the constructor then have a select case in the compare.

Something like:

Public Class PeopleComparer
Implements IComparer

Private Readonly m_field As String

Public Sub New()
MyClass.New("Name")
End Sub

Public Sub New(ByVal field As String)
m_field = field
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer _
Implements IComparer.Compare
Dim xo As People = DirectCast(x, People)
Dim yo As People = DirectCast(y, People)
Select Case m_field
Case "Sex"
Return xo.Sex.CompareTo(yo.Sex)
Case "Age"
Return xo.Age.CompareTo(yo.Age)
Case "Name"
Return xo.Name.CompareTo(yo.Name)
End Select
End Function

End Class
Array.Sort(MyPeopleArray, New PeopleComparer("Age"))

Hope this helps
Jay

"its me" <at**@house.com> wrote in message
news:4s*****************@fe02.atl2.webusenet.com.. .
Let's say I have a class of people...

Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class

And I declare an array of this class...

Dim MyPeopleArray(3) as People

Then I fill my arry with 3 people...

MyPeopleArray(0).Sex = "Female"
MyPeopleArray(0).Age = 32
MyPeopleArray(0).Name = "Marsha Marsha Marsha"

MyPeopleArray(1).Sex = "Male"
MyPeopleArray(1).Age = 31
MyPeopleArray(1).Name = "John"

MyPeopleArray(2).Sex = "Can't decide"
MyPeopleArray(2).Age = "23"
MyPeopleArray(2).Name = "Don't lable me"

How would I resort my the MyPeopleArray to have the people in Age order, name order or sex order?

Thanks!

Jim



Thanks for the reply, but this solution does not emulate the Array.Sort
function. ICompare only compares two objects and tells you which comes
first. It does not sort the array and does not compare more than 2

objects.
I can code a solution, I was just making sure that I wasn't re-inventing the wheel.

Thanks again.

Jim

Jul 19 '05 #4
Jay,

After some much needed sleep, I got to use your example.....and man did
it hit the spot.

Thanks again for your help!

Jim

Jul 19 '05 #5

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

Similar topics

4
by: Radhika Sambamurti | last post by:
Hi, I'm a relative newbie.... I've written a program to do a bubble sort. take in numbers and sort them. My question is: at the very end of the sort() function, when I have to output the result...
4
by: Todd | last post by:
I'm new to c++ and was wondering how to sort a 2 dimensional array. I'm using a select sort for 1 dimensional arrays but it is not working for a 2 dimensional array. The 2 dimensional array are...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
6
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
5
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
13
by: ANSHUL | last post by:
PLEASE PROVIDE ME D SOLUTION CODE FOR DIS PROBLEM. SELECTION SORT IS BASED ON D FOLLOWING IDEA: SELECTING D LARGEST ARRAY ELEMENT AND SWAPPING IT WITH THE LAST ARRAY ELEMENT LEAVES AN UNSORTED...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
4
by: VooDoo | last post by:
Hi, I am getting confused with sorting arrays... $arraytest =array(5) { =string(2) "39" =string(2) "44" => string(2) "77" =string(3) "150" =string(3) "464" } why do i get NULL value if i try to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.