Hmm... this sort function seems strangly similar to this.