JavaScript: Dynamic Sort