def sort_volumes
      # It only makes sense to sort if we have at least two volumes:
      if @volumes.length > 1
        raise "All volumes of the BinMatcher isntance must have the same number of BinImages (got lengths #{@volumes.collect {|v| v.bin_images.length}})." if @volumes.collect {|v| v.bin_images.length}.uniq.length > 1
        # Collect the Image references of the BinImage's of the first volume:
        desired_image_order = Array.new
        @volumes.first.bin_images.collect {|bin_image| desired_image_order << bin_image.image}
        raise "One (or more) Image references were nil in the first BinVolume instance of the BinMatcher. Unable to sort BinImages when they lack Image reference." if desired_image_order.compact.length != desired_image_order.length
        # Sort the BinImages of the remaining volumes so that their order of images are equal to that of the first volume:
        (1...@volumes.length).each do |i|
          current_image_order = Array.new
          @volumes[i].bin_images.collect {|bin_image| current_image_order << bin_image.image}
          sort_order = current_image_order.compare_with(desired_image_order)
          @volumes[i].bin_images.sort_by_order!(sort_order)
          #@volumes[i].reorder_images(sort_order)
        end
      end
    end
def sort_volumes
      # It only makes sense to sort if we have at least two volumes:
      if @volumes.length > 1
        raise "All volumes of the BinMatcher isntance must have the same number of BinImages (got lengths #{@volumes.collect {|v| v.bin_images.length}})." if @volumes.collect {|v| v.bin_images.length}.uniq.length > 1
        # Collect the Image references of the BinImage's of the first volume:
        desired_image_order = Array.new
        @volumes.first.bin_images.collect {|bin_image| desired_image_order << bin_image.image}
        raise "One (or more) Image references were nil in the first BinVolume instance of the BinMatcher. Unable to sort BinImages when they lack Image reference." if desired_image_order.compact.length != desired_image_order.length
        # Sort the BinImages of the remaining volumes so that their order of images are equal to that of the first volume:
        (1...@volumes.length).each do |i|
          current_image_order = Array.new
          @volumes[i].bin_images.collect {|bin_image| current_image_order << bin_image.image}
          sort_order = current_image_order.compare_with(desired_image_order)
          @volumes[i].bin_images.sort_by_order!(sort_order)
          #@volumes[i].reorder_images(sort_order)
        end
      end
    end
Rearranges the binary images belonging to the associated binary volumes
 (note that the master volume (if present) is excluded), by matching the
 binary images by their image instance references. This is done to ensure
 that the 3D NArrays which are extracted from these volumes are truly
 comparable.

 @raise Raises an exception if there are any irregularities in the number
   of BinImage instances or Image references.