Hypothetical sort question

B

B. Chernick

For no good reason I've written this little program and I've come upon an
interesting problem.

Assume that you've defined a class with multiple string and integer
properties. Now assume that you want to hold multiple instances of this
class in memory, in some sort of list/array/collection. (Use of a fixed
database would be too slow and there's no need to persist the data anyway.)

Now assume that you want to be able to sort the list on multiple different
string and/or integer properties. Does .Net have any built-in way to do
this?

(I should also mention that I've just started reviewing generics for a cert.
I'm not all that familiar with them. Want to be sure I'm not missing
something.)
 
K

Kerry Moorman

B. Chernick,

One option would be to implement the IComparer interface.

Kerry Moorman
 
C

Cor Ligthert[MVP]

Hi Charles,

Takes in Brittain a second the same time as at the continent?

:)

\\\
Module Module1

Sub Main()
Dim a As New Stopwatch
a.Start()
Dim myDataTable As New DataTable
Dim ID As New DataColumn("ID")
myDataTable.Columns.Add(ID)

For i As Integer = 0 To 100
Dim dr = myDataTable.NewRow
dr("ID") = DBNull.Value
myDataTable.Rows.Add(dr)
Next

For Each dr As DataRow In myDataTable.Rows
Dim MyValue As Integer

If dr("ID").Equals(DBNull.Value) Then
dr.SetColumnError("ID", "ID cannot be null") ' this take
1 to 2 seconds each time
Else
MyValue = CType(dr("ID"), Integer)
End If
Next
Console.WriteLine(a.ElapsedMilliseconds / 1000)
Console.ReadLine()
End Sub

End Module
///

Cor
 
B

Brian Gideon

B. Chernick,

One option would be to implement the IComparer interface.

Kerry Moorman

You could implement the IComparable interface as well. The List.Sort
method, for example, would then perform the operation based on the
implementation of that interface by default without having to pass in
an IComparer. Your solution is fine as well though.
 
K

Kerry Moorman

Brian,

I think I misunderstood the original question as wanting to sort on several
different properties independently. That situation is where I tend to use
IComparer.

But I think that the original question might have meant major, minor sort
fields.

Kerry Moorman
 
B

Brian Gideon

Brian,

I think I misunderstood the original question as wanting to sort on several
different properties independently. That situation is where I tend to use
IComparer.

But I think that the original question might have meant major, minor sort
fields.

Kerry Moorman

Yeah, I took it as sorting on those fields simultaneously. Either way
IComparer or IComparable would be sufficient.
 
B

Bill McCarthy

Hi B,

If you are using List(Of T) you can provide an inline comparer function. For
example, with a list of customers:


customers.Sort(Function(cust1, cust2)
cust1.LastName.CompareTo(cust2.LastName))


But you can also make the expression more complex such as


customers.Sort(Function(cust1, cust2) If(cust1.LastName = cust2.LastName,
cust1.FirstName.CompareTo(cust2.FirstName),
cust1.LastName.CompareTo(cust2.LastName)))

which sorts by last name then first name.


Alternatively if I want to replace the list and not sort in situ you can use
LINQ:

Dim sortedCustomers = (From c In customers Order By c.LastName,
c.FirstName).ToList
 
B

B. Chernick

Thanks to all who responded. As it turns out, I just needed my memory jogged
(and read another chapter of a book). I did something similar a few years
ago in Dot Net 1.1 with an ArrayList. Just declare a IComparer-derived class
and pass it to the ArrayList.Sort(). Right? (Although as I recall, we got
rid of this technique because it was a performance pig.)

So I suppose my answer to my own question (performance issues aside) would
be to do something similar with either an ArrayList or a generic List(of T)
and then declare as many Icomparer classes as needed, one for each scenario.
 

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

Top