in

ASP.NET Weblogs

MindFill - Brian Carroll's Blog

Do While (WhatYouKnow) < (WhatYouKnow + 1)

Sorting an Array of Structures

As you probably know, it's easy to sort an array in .NET using Array.Sort(). But what if you want to sort an array of structures on a particular field defined in the structure?  It can be done, and what follows is the approach I took.

I created a structure to represent a file:
  Private Structure SuccessFile
    Public Name As
String
   
Public Created As
Date
   
Public Hidden As
Boolean
  End Structure

Then I created an array to hold a list of files (and of course, I populated the array, but I'll leave that code out):
  Dim SuccessFiles(FileList.Count - 1) As SuccessFile

At this point I needed to sort the items in the array by the Created field.  I tried Array.Sort(), but it threw an exception, so I went to MSDN.

The MSDN description of Array.Sort says:
 "Sorts the elements in an entire one-dimensional Array using the IComparable interface implemented by each element of the Array."

That sounded like the array of structures could be sorted if the structure implemented the IComparable interface.  Cool!  .NET structures can implement interfaces, so this should work.

I modified my structure as follows, and the Array.Sort worked perfectly.

  Private
Structure SuccessFile
   
Implements
IComparable

    Public Name As
String
    Public Created As
Date
    Public Hidden As
Boolean

   
Public Function CompareTo(ByVal obj As Object) As Integer Implements
System.IComparable.CompareTo
     
Return Me.Created.CompareTo(CType
(obj, SuccessFile).Created)
   
End
Function
End
Structure

I hope you find this useful.

Published Jan 27 2004, 05:51 PM by bkcarroll
Filed under:

Comments

 

Scott said:

Yah, but you should add some type checking methinks instead of trying to convert it. It's better to check your inputs. A custom exception saying "wrong input type" makes much more sense than an "invalid cast Exception" I think. You could also just return false since objects of two different types can't be equal.

Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
if TypeOf obj Is SuccessFile
Return Me.Created.CompareTo(obj.Created)
else
throw new Exception("Input type must be SuccessFile")
end if
End Function
January 27, 2004 7:37 PM
 

damit@mvps.org (Damit) said:

I don't know if you already knew about this, but if you can't modify the structure itself to implement IComparable, you can use an IComparer with Array.Sort.

I did something similar to this in Java for a project (using reflection to implement sorting on multiple keys) and it worked quite well. :-)
January 28, 2004 1:26 AM
 

Brian Carroll said:

I agree with your suggestion about type checking. Of course I'm going to say that my code was for example purposes only, it's up to you if you want to believe me or not. :)

I'm also aware of IComparer, but have not used it.

Thanks for the feedback.
January 29, 2004 12:29 AM
 

B@@ said:

i was searching in Google for the answer on how to sort a filelist on the 3rd, 6th and 8th characters (in that order), you reckon i should use an arraylist + icomparer?
April 19, 2004 4:00 AM
 

Brian Carroll said:

I don't see why not. Googling for this string "Sorting ArrayList of Strings by the nth character" will get you off to a good start.

Good luck!
April 19, 2004 8:46 AM
 

Steven Vallarian said:

Thanks for the code, helped me out with my programming assignment. (If I have to write a bubble sort one more time I'm going to scream)

September 5, 2007 12:10 AM

Leave a Comment

(required)  
(optional)
(required)  
Add