Binarysearch


#1

There is a method Binarysearch in Rhinocommon which I dont understood!specifically the Icomparer thing, I dont get it! can some body explain how does it work?May be with an example!
That should be much faster than normal linear search I suppose


#2

There is a nice description in the Rhinoscript101 Primer - on pages 78-80…
HTH, --Mitch


(Steve Baer) #3

IComparer defines a function that is used to compare the items in the list for sorting purposes. There should be quite a few articles on the internet about this. Just search for “IComparer C#” and you will get a bunch of useful hits.

Here’s a good place to start if you want to use the Microsoft documentation to get started
http://msdn.microsoft.com/en-us/library/4ba2bttb.aspx


(Dale Fugier) #4

Here is a simple example that might be helpful:

def test_system_array():
    
    # Class that implements IComparer interface
    class MyComparer(System.Collections.IComparer):
        def Compare(self, x, y): return x.CompareTo(y)
        
    # Array to sort
    array = System.Array.CreateInstance(str, 6)
    array[0] = "Pachycephalosaurus"
    array[1] = "Amargasaurus"
    array[2] = "Tyrannosaurus"
    array[3] = "Mamenchisaurus"
    array[4] = "Deinonychus"
    array[5] = "Edmontosaurus"
    
    # Sort ascending
    System.Array.Sort(array, MyComparer())
    for i in range(array.Length):
        print array[i]
        
    # Search for something
    idx = System.Array.BinarySearch(array, "Deinonychus")
    print idx

#5

Thanks guys,
That was very helpful,I would try to test it myself see if I can use it or not .
Cheers