473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Hypothetical sort question

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.)
Sep 12 '08 #1
7 863
B. Chernick,

One option would be to implement the IComparer interface.

Kerry Moorman
"B. Chernick" wrote:
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.)
Sep 12 '08 #2
B. Chernick wrote:
>
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.)
Generics are the way to do this, and it is not difficult.

In the example class below, I define a Shared test function. This one compares
the integer first; if that is equal, it compares the string. With that in place,
you can use a standard generic list of your class, and the list's built in sort
method.

Public Class SomeClass

Public SomeText As String
Public SomeNumber As Integer

Public Sub New(ByVal Text As String, ByVal Number As Integer)
SomeText = Text
SomeNumber = Number
End Sub

Public Shared Function FancyTest( _
ByVal A As SomeClass, ByVal B As SomeClass) As Integer

FancyTest = A.SomeNumber.CompareTo(B.SomeNumber)
If FancyTest = 0 Then
FancyTest = A.SomeText.CompareTo(B.SomeText)
End If

End Function

End Class

Private Sub Button5_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button5.Click

Dim SomeList As New List(Of SomeClass)

SomeList.Add(New SomeClass("World", 2))
SomeList.Add(New SomeClass("Hello", 1))
SomeList.Add(New SomeClass(", ", 2))

SomeList.Sort(AddressOf SomeClass.FancyTest)

For Each o As SomeClass In SomeList
Debug.Write(o.SomeText)
Next
Debug.Print("")

End Sub
Sep 13 '08 #3
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

Sep 13 '08 #4
Sorry,

Was meant for Charles Law, wrong thread

cor
Sep 13 '08 #5
On Sep 12, 3:46*pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.comwrote:
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.
Sep 13 '08 #6
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
"Brian Gideon" wrote:
On Sep 12, 3:46 pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.comwrote:
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.
Sep 13 '08 #7
On Sep 13, 2:49*pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.comwrote:
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.
Sep 13 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gabriel | last post by:
I have a hypothetical question for someone at Microsoft to answer. Let's say I have built my own proprietary language. I also have a small following of developers, and I have made some money...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.