Sorting arrays

  • Thread starter Thread starter Najm
  • Start date Start date
N

Najm

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
 
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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top