# Mathematical Operations on images in Google Earth Engine not matching bandnames?

I'm working on a set of images that have the same bandnames, but are present in a different order in each of the images. Following the API documentation I was in the understanding that bands would be matched by name, e.g. for `ee.Image.subtract()`:

Subtracts the second value from the first for each matched pair of bands in image1 and image2. If either image1 or image2 has only 1 band, then it is used against all the bands in the other image. If the images have the same number of bands, but not the same names, they're used pairwise in the natural order.

However, the order of the bands, not the bandnames themselves, seems the way the bands are matched.

``````// Mock ROI
var ROI = ee.Geometry.Polygon([[8,47], [9,47], [9,49], [8,49], [8,47]], null, false)

// Image 1: 3 random bands. Band 4 is constant with a value of 1
var image1 = ee.Image.random(1)
.rename(['b1', 'b2', 'b3', 'b4'])

// Image 2: again 3 random bands and band 4 constant with value of 1. But bands in different order.
var image2 = ee.Image.random(11)
.rename(['b1', 'b2', 'b4', 'b3'])

// Subtract image2 from image1
var subtracted = image1.subtract(image2)

// Print mean value in ROI - for 'b4' this *should* equal 0 (1 minus 1)
print('Mean value, unsorted bands', subtracted.reduceRegion({reducer: ee.Reducer.mean(), geometry: ROI, scale: 1e3}))
// >>> b4: 0.5004240454234745

// Sort the bands
var image2_sorted = image2.select(['b1', 'b2', 'b3', 'b4'])

// Subtract sorted image2 from image1
var subracted_sorted = image1.subtract(image2_sorted)

// Print mean value in ROI - for 'b4' this *should* equal 0 (1 minus 1)
print('Mean value, sorted bands', subracted_sorted.reduceRegion({reducer: ee.Reducer.min(), geometry: ROI, scale: 1e3}))
// >>> b4: 0
``````

The same seems to be happening for other mathematical operators like `.add()`, `.multiply()`, `.divide()`, etc.

Of course, the fix is straightforward (just make sure that all bands are in the same order). But I'm mostly trying to understand why the bands aren't matched by name? Maybe I'm just missing something obvious and this is the expected behaviour?