Monday, May 05, 2003 2:04 PM Jan Tielens

Simple Comparer for sorting in VB.NET

I don't know if something like this exists, or even is available in the .NET Framework itself, but I had to make a routine to sort a collection of objects, so I built a simple comparer class. The comparer class can be used like this:
Dim customers As New ArrayList

'Or you can use the Sort method of the strong typed collection,
'inheriting from CollectionBase.

customers.Sort(New SimpleComparer("Name"))
'or
customers.Sort(New SimpleComparer("Name", SortOrder.Descending))





The complete code for SimpleComparer class is:
    Public Class SimpleComparer

Implements IComparer

Private _propertyToSort As String
Private _sortOrder As SortOrder

Public Sub New(ByVal propertyToSort As String)
Me.new(propertyToSort, System.Windows.Forms.SortOrder.Ascending)
End Sub

Public Sub New(ByVal propertyToSort As String, ByVal sortOrder As SortOrder)
MyBase.new()
_propertyToSort = propertyToSort
_sortOrder = sortOrder
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Dim prop As Reflection.PropertyInfo = x.GetType.GetProperty(Me.PropertyToSort)

If Me.SortOrder = SortOrder.None OrElse prop.GetValue(x, Nothing) = _
prop.GetValue(y, Nothing) Then
Return 0
Else
If prop.GetValue(x, Nothing) > prop.GetValue(y, Nothing) Then
If Me.SortOrder = System.Windows.Forms.SortOrder.Ascending Then
Return 1
Else
Return -1
End If
Else
If Me.SortOrder = System.Windows.Forms.SortOrder.Ascending Then
Return -1
Else
Return 1
End If
End If
End If
End Function

Public Property SortOrder() As SortOrder
Get
Return _sortOrder
End Get
Set(ByVal Value As SortOrder)
_sortOrder = Value
End Set
End Property

Public Property PropertyToSort() As String
Get
Return _propertyToSort
End Get
Set(ByVal Value As String)
_propertyToSort = Value
End Set
End Property
End Class


Comments

# re: Simple Comparer for sorting in VB.NET

Monday, May 05, 2003 1:14 AM by Dana Coffey

VERY slick! That one is going in my library :)

# re: Simple Comparer for sorting in VB.NET

Tuesday, July 01, 2003 12:47 AM by randy collins

Thanks for sharing this! I'm new to .NET and this is an immense help.

# re: Simple Comparer for sorting in VB.NET

Thursday, December 11, 2003 4:31 PM by Wayde Wyatt

Wow, that's a nice piece of work. Thanks so much!!

# re: Simple Comparer for sorting in VB.NET

Tuesday, January 27, 2004 11:34 PM by schneider

try setting Option Strict On ......

# re: Simple Comparer for sorting in VB.NET

Tuesday, March 16, 2004 4:45 AM by Thierry

A great trick, thanks !!!

# re: Simple Comparer for sorting in VB.NET

Friday, April 30, 2004 6:14 AM by Paul McEwan

Thanks! just what I was looking for =)

# re: Simple Comparer for sorting in VB.NET

Friday, January 19, 2007 7:08 PM by [3!]ferta,[3!]ferta

Your site looks great!!!!!!!!!! Please, look at my ;) Thanks!,Your site looks great!!!!!!!!!! Please, look at my ;) Thanks!

# re: Simple Comparer for sorting in VB.NET

Friday, March 23, 2007 2:59 PM by Leonardo

Nice class!!! Really really helpful! Thanks!

# re: Simple Comparer for sorting in VB.NET

Tuesday, March 27, 2007 4:39 AM by Heel

<a href='http://portalesvirtualesgratis.androot.info/'>portales virtuales gratis</a>  [URL=http://portalesvirtualesgratis.androot.info/ ]portales virtuales gratis[/URL]  

<a href='http://dbzmugen.androot.info/'>dbz mugen</a>  [URL=http://dbzmugen.androot.info/ ]dbz mugen[/URL]  

<a href='http://www.putaria.com.br.androot.info/'>www.putaria.com.br</a>  [URL=http://www.putaria.com.br.androot.info/ ]www.putaria.com.br[/URL]  

<a href='http://salutedellapelle.androot.info/'>salute della pelle</a>  [URL=http://salutedellapelle.androot.info/ ]salute della pelle[/URL]  

<a href='http://nataliciodebenitojuarez.androot.info/'>natalicio de benito juarez</a>  [URL=http://nataliciodebenitojuarez.androot.info/ ]natalicio de benito juarez[/URL]  

<a href='http://ilpompino.androot.info/'>il pompino</a>  [URL=http://ilpompino.androot.info/ ]il pompino[/URL]  

<a href='http://traductoresgratis.androot.info/'>traductores gratis</a>  [URL=http://traductoresgratis.androot.info/ ]traductores gratis[/URL]  

<a href='http://fotosdemulheresbrasileirasnuas.androot.info/'>fotos de mulheres brasileiras nuas</a>  [URL=http://fotosdemulheresbrasileirasnuas.androot.info/ ]fotos de mulheres brasileiras nuas[/URL]  

<a href='http://guardianangelbeckhamtattoo.androot.info/'>guardian angel beckham tattoo</a>  [URL=http://guardianangelbeckhamtattoo.androot.info/ ]guardian angel beckham tattoo[/URL]  

<a href='http://videodeninfetasnuas.androot.info/'>video de ninfetas nuas</a>  [URL=http://videodeninfetasnuas.androot.info/ ]video de ninfetas nuas[/URL]  

# re: Simple Comparer for sorting in VB.NET

Friday, March 30, 2007 10:14 PM by Britneywhutz

Very nice! I have some LJ with news, check this out:

<a href= http://michelas.livejournal.com >My live journal</a>

<a href= http://homerius.livejournal.com >Lastest news</a>

<a href= http://johnyknoxw.livejournal.com >My live journal</a>

# re: Simple Comparer for sorting in VB.NET

Sunday, April 15, 2007 2:02 AM by Britneyuntyf

<a href= http://www.angelfire.com/planet/sahegi >aaa disneyland packages</a> <a href= http://www.angelfire.com/poetry/moponu >aac audio file format</a> <a href= http://www.angelfire.com/poetry/xosazy >a quintessential</a> <a href= http://www.angelfire.com/indie/dulyry >a 46146</a> <a href= http://www.angelfire.com/goth/pomezo >aaralyn barra myspace.com site</a>

# re: Simple Comparer for sorting in VB.NET

Sunday, April 15, 2007 2:02 AM by Britneyuntyf

<a href= http://www.angelfire.com/planet/sahegi >aaa disneyland packages</a> <a href= http://www.angelfire.com/poetry/moponu >aac audio file format</a> <a href= http://www.angelfire.com/poetry/xosazy >a quintessential</a> <a href= http://www.angelfire.com/indie/dulyry >a 46146</a> <a href= http://www.angelfire.com/goth/pomezo >aaralyn barra myspace.com site</a>

# re: Simple Comparer for sorting in VB.NET

Sunday, April 15, 2007 2:02 AM by Britneybsqkz

<a href= http://www.angelfire.com/goth/wagoly >aalborgs</a> <a href= http://www.angelfire.com/crazy/jukana >a level revision notes</a> <a href= http://www.angelfire.com/blog/wimide >a lot of love trailer</a> <a href= http://www.angelfire.com/funky/xocizo >aacharity.com car charitable charitable charity donate donation donation</a> <a href= http://www.angelfire.com/blog/dycyfe >a2 golf</a>

# re: Simple Comparer for sorting in VB.NET

<a href=" http://galadriel.nmsu.edu/store/Buy-Viagra.html ">Buy Generic Viagra</a>

# re: Simple Comparer for sorting in VB.NET

Sunday, April 22, 2007 4:21 PM by Britneygkxrf

<a href= http://www.angelfire.com/droid/kimihu >a668</a> <a href= http://www.angelfire.com/planet/gocuki >a9250 driver</a> <a href= http://www.angelfire.com/indie/lupysu >a55 code master siemens</a> <a href= http://www.angelfire.com/punk/danuge >aam perkhidmatan suruhanjaya</a> <a href= http://www.angelfire.com/funky/myguwu >aaa reservation travel</a>

# re: Simple Comparer for sorting in VB.NET

Thursday, May 10, 2007 9:14 PM by Britneyzzssp

<a href= http://poquhe.front.ru >a company call pds parcel</a> <a href= http://fynezi.front.ru >a bird of prey jim grimsley</a> <a href= http://walala.front.ru >a muscleman</a> <a href= http://gutuxa.front.ru >a mummy shaped amulet that held farm tools</a> <a href= http://kybake.front.ru >a very good production inc.</a>

# re: Simple Comparer for sorting in VB.NET

Wednesday, May 30, 2007 7:43 PM by rigjif

gserfdrfsfsfwttwtgtt6tdrytstdtstrdttsttdtsttdstd5wtetwt6ruuigkjriuuyyewttetwttwtetyrtrkjgkfjgjhhwwywyye

h

sgf

gs

dfs

g

gd

sg

sg

d

sg

d

gsh

h

gdshdhshdshdhsh

hs

hs

hs

hs

h

shh

ssh

hs

hs

hss

hs

hs

shs

f

f

ff

f

ff

f

f

ff

f

f

ff

f

f

f

# re: Simple Comparer for sorting in VB.NET

Wednesday, May 30, 2007 7:43 PM by rigjif

gserfdrfsfsfwttwtgtt6tdrytstdtstrdttsttdtsttdstd5wtetwt6ruuigkjriuuyyewttetwttwtetyrtrkjgkfjgjhhwwywyye

h

sgf

gs

dfs

g

gd

sg

sg

d

sg

d

gsh

h

gdshdhshdshdhsh

hs

hs

hs

hs

h

shh

ssh

hs

hs

hss

hs

hs

shs

f

f

ff

f

ff

f

f

ff

f

f

ff

f

f

f

# re: Simple Comparer for sorting in VB.NET

Wednesday, June 27, 2007 1:31 PM by Kyle

Thanks for the info.

FYI ... I've extended this a little bit, to be able to sort "unsortable" collections with nested classes as items...

geekswithblogs.net/.../65680.aspx

Thanks,