Sorting like that won't produce consistent results. If EA is present in the original hash but not AE, you'll end up with EA instead of AE. Sort the chars of the key rather than the order in which they are fetched to avoid guessing under which key a particular value is located.