I have the following code:

public static Map<Id,Location__c> getAllStoresInRegion(string currRegion){

    LocationService locationService = new LocationService();
    Map<Id, Location__c> filteredLocationMap = filterStoreMap(currRegion, locationService.getActiveMap());
    return filteredLocationMap;

private static Map<Id, Location__c> filterStoreMap(string currRegion, Map<Id, Location__c> locationMap){
    Map<Id, Location__c> filteredLocationMap = locationMap;
    List<String> excludedStores = new List<String>();


    for(Location__c location : locationMap.values()){
        if(location.Region__c != currRegion || excludedStores.contains(location.Store_Number__c)){
    return filteredLocationMap;

I want the function filterStoreMap() to also sort by Location__c.Name (ascending) in the map that it returns. While I am clear on filtering data, not very clear on the sorting. I saw some mention of comparable interface but that seems a bit complicated. Any tips?

  • 1
    You can't sort a Map, neither in Apex or JavaScript., as Map objects are an unordered collection You will need to use a List/Array instead.
    – sfdcfox
    Commented Mar 16, 2020 at 20:55

1 Answer 1


Because sObjects have some default sorting rules, you won't need a comparible. Default sort order is:

The label of the sObject type. For example, an Account sObject will appear before a Contact.

The Name field, if applicable. For example, if the list contains two accounts named A and B respectively, account A comes before account B.

Ref here

So, you could grab all the location values, sort em and put them back into the map. Like this:

Location__c[] locations = filteredLocationMap.values();
Map<Id, Location__c> sortedLocationMap = new Map<Id,Location>(locations);
  • This won't work; the order will be wiped out once the JavaScript gets a hold of the data. Maps do not have a notion of ordering, and the act of serializing and deserializing will result in the order being lost.
    – sfdcfox
    Commented Mar 17, 2020 at 0:03
  • Fair enough, but it will work if he just returns the list of locations to JS. Or returns a list of locations ids + the map. Main point, it's sorted and it's not that hard for it to remain sorted. Commented Mar 17, 2020 at 0:20
  • The initial map was created as a SOQL query 'order by name asc'. It then was replaced by a service so while I can filter the results as I am above, I am still unclear if the approach above would result in the locations to be sorted by name asc. Sorry I cant just return a list in that method as it would involve additional refactoring.
    – kratos
    Commented Mar 17, 2020 at 16:13
  • 1
    If you really really can't alter the signature of that method, put a long text area onto your location object and serialize your sorted location array into that. Then pull it out in js. It's massive hack, but as long as you don't save to the db, you'll be ok. Commented Mar 17, 2020 at 18:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .