function sort( a, b ) { return a.length>b.length; } m = "P3P4P5P6P7vP5P3P1vP2P8P6vP3P2P9vP5P6vP6"; arr = m.split( 'v' ); arr.sort( sort );