Fix Issue 4493 - Add sorting capability to toJSON and add another unittest.
Merge pull request #2120 from AndrejMitrovic/Fix4493

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