C # dictionary for json - sorts automatically

I work in MVC. This is a $ .AJAX post, and I am returning a dictionary that is already sorted by key. Then I pass this dictionary as a result of JSON.

But in the browser, what I get is the same dictionary, but sorted in ascending order by key.

Is there a reason why it is sorted differently or if this is some kind of problem, please let me know what to do to get a dictionary with the same sorted order.

I already have googled and I have not found anything related to this.

Regards, Venkatesan R

+6
source share
1 answer

The dictionary is not ordered. This is normal, because JSON objects (and corresponding JavaScript objects) are also unordered 1 maps of key / value pairs.

Instead, use a JSON array (e.g., mapped to a List) to maintain an ordered sequence.


1 Unfortunately, the returned data (or possibly display) is displayed sorted in a certain order, since there is no such guarantee.

See also:

+5
source

Source: https://habr.com/ru/post/957544/


All Articles