.NET BinarySearch() on ArrayList of custom objects
Posted
by
Alex
on Stack Overflow
See other posts from Stack Overflow
or by Alex
Published on 2011-01-03T18:38:59Z
Indexed on
2011/01/03
18:54 UTC
Read the original article
Hit count: 209
Hi. I have an ArrayList of custom objects that have the following properties:
- FileName
- FilePath
- CurrentFolder
- TopLevelFolder
I then need to do a BinarySearch (or some other quick search) on the FileName property on all the objects in the ArrayList in .NET. In other words, I need to find the object in the ArrayList with the same FileName as the one I'm searching on.
Syntax for the ArrayList's BinarySearch is this; but how do you do this for an object's property in the arraylist?
public static void FindMyObject( ArrayList myList, Object myObject ) {
int myIndex=myList.BinarySearch( myObject );
if ( myIndex < 0 )
Console.WriteLine( "The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, ~myIndex );
else
Console.WriteLine( "The object to search for ({0}) is at index {1}.", myObject, myIndex );
}
© Stack Overflow or respective owner