Actually I figured it out. Sort function almost finished now.