Here is SpringMVC Controller code snippet:
@RequestMapping(value = "/getCityList", method = RequestMethod.POST) public @ResponseBody LinkedHashMap<String, String> getCityList(@RequestParam(value = "countryCode") String countryCode, HttpServletRequest request) throws Exception { //gets ordered city list of country [sorted by city name] LinkedHashMap<String, String> cityList = uiOperationsService.getCityList(countryCode); for (String s : cityList.values()) { System.out.println(s); //prints sorted list [sorted by name] } return cityList; } Here is ajax call:
function fillCityList(countryCode) { $.ajax({ type: "POST", url: '/getCityList', data: {countryCode:countryCode}, beforeSend:function(){ $('#city').html("<option value=''>-- SELECT --</option>" ); } }).done(function (data) { console.log(data); // UNSORTED JSON STRING [Actually sorted by key... not by city name] }) } Sorted LinkedHashMap returns as unsorted JSON object from getCityList method. Why order is changed during return process ? Is LinkedHashMap converted to HashMap because of ResponseBody annotation? I can convert my sorted object to json string via Gson library and return json string from my getCityList method but i don't like this solution. What can i do to provide javascript callback method with sorted list?