The invention describes a method for isolating one or more genetic elements encoding a gene product having a desired activity, comprising the steps of: (a) compartmentalising genetic elements into microcapsules; (b) expressing the genetic elements to produce their respective gene products within the microcapsules; (c) sorting the genetic elements which produce the gene product having the desired activity using a change in the optical properties of the genetic elements. The invention enables the in vitro evolution of nucleic acids and proteins by repeated mutagenesis and iterative applications of the method of the invention.