Implementation of sorts in javascript