Sort Arrays

  • Thread starter Thread starter HardySpicer
  • Start date Start date
H

HardySpicer

I have 7 string arrays whose indexes must always be related to one
another since they are part of what is similar to a spread-sheet type
thing. I wish to sort the first array in alphabetical order - and the
others - since they are linked must follow suit so that the rows are
all in the same order as the first. I can use arrays.sort() I suppose
but this only sorts the first array - how do I "sync" them!


regards

H.
 
HardySpicer said:
I have 7 string arrays whose indexes must always be related to one
another since they are part of what is similar to a spread-sheet type
thing. I wish to sort the first array in alphabetical order - and the
others - since they are linked must follow suit so that the rows are
all in the same order as the first. I can use arrays.sort() I suppose
but this only sorts the first array - how do I "sync" them!

You should have a single array of custom objects instead of spreading
the data across arrays. That way you can easily sort the array without
worrying about losing the connection between the properties.

Example:

Public Class Company

Private _name As String
Private _address As String
Private _zip As String
Private _city As String

Public Readonly Property Name
Get
Return _name
End Get
End Property

Public Readonly Property Address
Get
Return _address
End Get
End Property

Public Readonly Property Zip
Get
Return _zip
End Get
End Property

Public Readonly Property City
Get
Return _city
End Get
End Property

Public Sub New(name As String, address As String, zip As String,
city As String)
_name = name
_address = address
_zip = zip
_city = city
End Sub

Public static Function CompareNames(c1 As Company, c2 As Company) As
Integer
Return String.Compare(c1.Name, c2.Name)
End Function

End Class


Dim compaines(2) As Company
companies(0) = New Company("Movers", "Road 1", "1234", "Grove")
companies(1) = New Company("Shakers", "Road 4", "4321", "Town")

Array.Sort<Company>(companies, Company.CompareNames)
 
You should have a single array of custom objects instead of spreading
the data across arrays. That way you can easily sort the array without
worrying about losing the connection between the properties.

Example:

Public Class Company

Private _name As String
Private _address As String
Private _zip As String
Private _city As String

Public Readonly Property Name
Get
Return _name
End Get
End Property

Public Readonly Property Address
Get
Return _address
End Get
End Property

Public Readonly Property Zip
Get
Return _zip
End Get
End Property

Public Readonly Property City
Get
Return _city
End Get
End Property

Public Sub New(name As String, address As String, zip As String,
city As String)
_name = name
_address = address
_zip = zip
_city = city
End Sub

Public static Function CompareNames(c1 As Company, c2 As Company) As
Integer
Return String.Compare(c1.Name, c2.Name)
End Function

End Class

Dim compaines(2) As Company
companies(0) = New Company("Movers", "Road 1", "1234", "Grove")
companies(1) = New Company("Shakers", "Road 4", "4321", "Town")

Array.Sort<Company>(companies, Company.CompareNames)

Clever stuff!
 
And if you want to save yourself some typing you throw a dataset in your
project , go to the design view of the dataset add a datatable and all the
fields
you want , declare a pointer to the dataset in your project and ready you
are .

You can now sort , on anny field , add items , remove items , update items ,
use LINQ on it etc etc etc and with a minimum of having to write code as the
dataset designer does everything for you behind the scenes


regards

Michel


"HardySpicer" <[email protected]> schreef in bericht
You should have a single array of custom objects instead of spreading
the data across arrays. That way you can easily sort the array without
worrying about losing the connection between the properties.

Example:

Public Class Company

Private _name As String
Private _address As String
Private _zip As String
Private _city As String

Public Readonly Property Name
Get
Return _name
End Get
End Property

Public Readonly Property Address
Get
Return _address
End Get
End Property

Public Readonly Property Zip
Get
Return _zip
End Get
End Property

Public Readonly Property City
Get
Return _city
End Get
End Property

Public Sub New(name As String, address As String, zip As String,
city As String)
_name = name
_address = address
_zip = zip
_city = city
End Sub

Public static Function CompareNames(c1 As Company, c2 As Company) As
Integer
Return String.Compare(c1.Name, c2.Name)
End Function

End Class

Dim compaines(2) As Company
companies(0) = New Company("Movers", "Road 1", "1234", "Grove")
companies(1) = New Company("Shakers", "Road 4", "4321", "Town")

Array.Sort<Company>(companies, Company.CompareNames)

Clever stuff!
 
<SNIP>
GASP!!!
</SNIP>

SIGH !!!!!

and

AYBABTU !!!!!

“You are blind and I am deaf and dumb, so let us touch hands and understand”

regards

Michel
 
Back
Top