Fix Issue 4493 - Add sorting capability to toJSON and add another unittest.