Board index » Visual Studio » Sorting arrays

Sorting arrays

Visual Studio272
Hi,

How would it be possible to sort an array of objects (my_class) based on one

attribute of that class?

suppose the class' attributes are the following

my_class:

attr1 as integer

attr2 as str





Thanks


-
 

Re:Sorting arrays

Najm,



In my opinion is the most simple solution to use a datatable or use a

collection.



I take always the first, however there are people who have difficult classes

or want to do some more work and than there is to create a collection class

a complete sample.



http://msdn.microsoft.com/library/default.asp?url" rel="nofollow" target="_blank">msdn.microsoft.com/library/default.asp=/library/en-us/vbcon/html/vaconCreatingYourOwnCollectionClass.asp



In my opinion will all other approaches lead to a lot of overhead.



Cor





-

Re:Sorting arrays

Thanks for ur help,

if i understood, u suggest i use a datatable to store the data instead of

the array of that object.

I have another question:

suppose i bind that datatable to a datagrid. is there a way to have indexes

of the table stay the same as the datagrid in the case of a datagrid sorting

?

thanks







"Cor Ligthert" <notmyfirstname@planet.nl>wrote in message

Quote
Najm,



In my opinion is the most simple solution to use a datatable or use a

collection.



I take always the first, however there are people who have difficult

classes

or want to do some more work and than there is to create a collection

class

a complete sample.





http://msdn.microsoft.com/library/default.asp?url" rel="nofollow" target="_blank">msdn.microsoft.com/library/default.asp=/library/en-us/vbcon/html/

vaconCreatingYourOwnCollectionClass.asp

Quote


In my opinion will all other approaches lead to a lot of overhead.



Cor









-

Re:Sorting arrays

"Najm" <n_hadrouni@hotmail.com>schrieb:

Quote
How would it be possible to sort an array of objects (my_class) based on

one

attribute of that class?

suppose the class' attributes are the following

my_class:

attr1 as integer

attr2 as str



CompareOperator

<URL:dotnet.mvps.org/dotnet/samples/codingtechnique/CompareOperator.zip>">dotnet.mvps.org/dotnet/samples/codingtechnique/CompareOperator.zip>



--

M S Herfried K. Wagner

M V P <URL:dotnet.mvps.org/>">dotnet.mvps.org/>

V B <URL:dotnet.mvps.org/dotnet/faqs/>">dotnet.mvps.org/dotnet/faqs/>



-

Re:Sorting arrays

Najm,



That is standard, assuming it is a windowform datagrid. Than holds the

"sort" item the current sort method of the datatable.defaultview or a

dataview which is the datasource of your datagrid.



The currencymanager.postition is the item that you can use to get the

currentrow of a dataview.



dv(BindingContext(dv, currencymanager).Position)(0) holds the first field in

the current row



I hope this helps?



Cor





-

Re:Sorting arrays

Try this:



Sub AddSort()



Dim w1 As New wantsort



Dim w2 As New wantsort



Dim w3 As New wantsort



Dim al As New ArrayList



Dim ic As New wantsortComparerName



w1.attr1 = 2



w2.attr1 = 3



w3.attr1 = 1



al.Add(w1)



al.Add(w2)



al.Add(w3)



al.Sort(ic)



End Sub



End Class



Public Class wantsortComparerName



Implements IComparer







Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer

Implements System.Collections.IComparer.Compare



Dim xEntity As wantsort = x



Dim yEntity As wantsort = y



If xEntity.attr1 = yEntity.attr1 Then



Return 0



ElseIf xEntity.attr1 < yEntity.attr1 Then



Return -1



Else



Return 1



End If



End Function



End Class







Public Class wantsort



Public attr1 As Integer



Public attr2 As String



End Class



Good luck.

chanmm





"Najm" <n_hadrouni@hotmail.com>wrote in message

Quote
Hi,

How would it be possible to sort an array of objects (my_class) based on

one

attribute of that class?

suppose the class' attributes are the following

my_class:

attr1 as integer

attr2 as str





Thanks













-