I have the following divs:
test
test
First you need to create an array containing all the id values, then use Math to get the highest/lowest:
var ids = $(".maindiv[id]").map(function() {
return parseInt(this.id, 10);
}).get();
var highest = Math.max.apply(Math, ids);
var lowest = Math.min.apply(Math, ids);
Example fiddle
Note the [id] attribute selector is required, otherwise 0 is assumed for the missing value.