By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

Array.sort question

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.