Skip to main content
15 votes
Accepted

How to sort Map by values

Your best bet is to write a wrapper for this. public class AddressWrapper implements Comparable { public Address address; public Integer counter; public AddressWrapper(Address address, ...
sfdcfox's user avatar
  • 497k
7 votes
Accepted

Is it possible to sort this SOQL statement in one query?

The ordering that looks like [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 3, 4, 5, 6, 7, 8, 9] is called lexicographic ordering. When you have a mix of alpha and numeric characters, that's the ...
Derek F's user avatar
  • 62.7k
6 votes
Accepted

Sort wrapper list in Apex based on Levels

public class PartnerWrapper implements Comparable { private static final Map<String, Integer> levelsOrder = new Map<String, Integer>{ 'Platinum' => 1, 'Gold' => 2,...
Oleksandr Berehovskyi's user avatar
6 votes
Accepted

Descending Table With Two Objects

The default sorting algorithm doesn't work like that. You need to implement Comparable to do what you want. See Sorting Lists of sObjects: Default Sort Order of sObjects The List.sort method ...
Adrian Larson's user avatar
  • 151k
6 votes
Accepted

Cannot sort on numeric field with SOQL in Custom Metadata

You can only order by a field that's in the SELECT clause of the query for custom metadata types -- see p.3 of the Custom Metadata Types Implementation Guide for more information on query limitations.
Avrom Roy-Faderman's user avatar
6 votes

How to sort a list of strings in the descending order?

Apex has a Version class that supports major.minor and major.minor.patch numbering. If you work with those objects: List<Version> src = new List<Version>{ new Version(7, 1, 2), ...
Keith C's user avatar
  • 136k
5 votes

Decrement Max Value From Map?

Maps are an example of an unordered collection, which cannot be sorted. However, If you're open to writing code and spending additional time developing your idea, you can build something that should ...
Derek F's user avatar
  • 62.7k
5 votes
Accepted

Can I change Product lookup sort order?

You can't remove the field, nor can you really do anything else to it. However, if Product Name is unimportant, I'd suggest the following setup. First, go to Setup > Customize > Tab Names and Labels >...
sfdcfox's user avatar
  • 497k
5 votes

Apex equivalent of Java Comparator

As requested in the idea Support apex List sorting using a Comparator, the Winter '24 release brings a native Comparator interface and corresponding List sort method overload to work with it. You can ...
Daniel Ballinger's user avatar
5 votes

How to sort a list of strings in the descending order?

It's doing exactly what you asked it to do: it reversed the order of the list, which is what that for loop does. If you want to sort the strings, you need to perform a sort operation by calling List....
David Reed's user avatar
  • 93.2k
5 votes
Accepted

Sorting a List of different object Lists by datetime

You can do this using the standard sort() method provided by the List type if your ListWrapper class implements the Comparable interface You'll have to decide for yourself how to compare two ...
Derek F's user avatar
  • 62.7k
5 votes
Accepted

SOQL SORTING WITH 2 FIELDS

Generally speaking, when I have a question, the first place I look is at the documentation. Ordering by multiple columns is listed in ORDER BY in the SOQL & SOSL documentation (emphasis mine) ...
Derek F's user avatar
  • 62.7k
5 votes
Accepted

I'm trying to get my map in a certain order based on my list. But the map always return sorted values

This is a side effect of the "Critical Update: Predictable Iteration Order for Apex Unordered Collections" and the subsequent patch "Iteration Order for Maps and Sets Is Now Predictable". The effects ...
sfdcfox's user avatar
  • 497k
5 votes
Accepted

LWC Datatable sort - how do I not use a url to sort name hyperlink column?

In the sort method just set the sortBy conditionally. updateSort(event) { let fieldName = event.detail.fieldName; fieldName = fieldName === 'nameUrl' ? 'Name' : fieldName; let ...
Rahul Gawale's user avatar
  • 12.2k
5 votes

Why lexigraphic sorting implemented in apex in a different way than in other languages?

In Apex, == is a case-insensitive, locale-aware comparison of strings. That means that 'Hello' == 'hello' returns true. If something is equal to something else, neither are greater than or less than ...
sfdcfox's user avatar
  • 497k
4 votes
Accepted

Significant elapsed time differences when calling sort() on different Comparable implementations

All, I'm sorry to have wrapped everyone's minds around this today. It was definitely Apex logging levels that was corrupting my results, and now I feel slightly dumb and ever slightly more educated. ...
Robert Watson's user avatar
4 votes
Accepted

How to sort a dynamic picklist

Since you are creating the List based on query, so better to use ORDER BY clause for sorting. Here is an example. Nominators = [Select Id, Name, Class_Year__c, Contact_Name__c, ...
Santanu Boral's user avatar
4 votes

Sort a Map values apex

There is a work around that I always use for such case. Take keySet() of the map. Convert the set from Step 1. to List and sort it. Iterate through sorted list and for each element display map.get(...
Vladimir Egikyan's user avatar
4 votes

Efficiently sorting a list of Work Order records by a Sequence

As I've said ... at ... least ... four times, just because you have a for loop inside another for loop, does not automatically make this a Bad Thing. Feel free to read the answers for more information....
sfdcfox's user avatar
  • 497k
4 votes

Why is the sort pointer in the lightning datatable always pointing downwards in my case?

The Datatable documentation actually has an example specific to sorting you'll find helpful. Taking a quick look, I'd use the parameter in the click event to get the sort direction versus trying to ...
Kris Goncalves's user avatar
4 votes
Accepted

Do indexed fields guarantee sort optimisation?

First, let's start off by saying a lot of the engineering of databases at the scale of Salesforce, and most commercial CRMs, are typically far beyond the likes of anything other kinds of developers ...
sfdcfox's user avatar
  • 497k
3 votes
Accepted

Client side sorting based on Parent Fields

That code wasn't designed to handle parent path, but the sort function could be updated to support this: ({ sortBy: function(component, field) { var sortAsc = component.get("v.sortAsc"), ...
sfdcfox's user avatar
  • 497k
3 votes
Accepted

Sort a list of tasks by date value

Try this - list <task> Tareas_abiertas = [SELECT Id from Task where OwnerId =:usuario.id AND status='Open' Order By ActivityDate]; And from this Salesforce help article you can see that the ...
Jitender Padda's user avatar
3 votes
Accepted

Sorting a keySet

Sets are never sorted. Instead, expose the list directly to Visualforce, and then iterate over each key value. <apex:repeat value="{!sortedKeys}" var="key"> <apex:repeat value="{!caseMap[...
sfdcfox's user avatar
  • 497k
3 votes

Dynamic sorting in related lists in Salesforce classic

The only way to sort a related list is by setting the related sort options in the related lest settings on the page layout interface.
Carlos Naranjo's user avatar
3 votes
Accepted

Lightning client side sorting (using a separate function, not inline)

You're not supposed to call the function, but pass it in as a parameter to sort: cases.sort(self.sortByCaseNumber); Note the lack of parentheses, because we're not actually calling the function, ...
sfdcfox's user avatar
  • 497k
3 votes
Accepted

How to sort formula showing Image in List View

The formula is going to end up being sorted alphabetically by file name. You would necessarily need to prefix the formula with some sort of text that can be sorted. There's absolutely no way to have ...
sfdcfox's user avatar
  • 497k
3 votes
Accepted

How to sort a list of strings in the descending order?

This is a general approach I'd suggest you consider taking considering the nature of your data. If you don't want to use comparable, you will need to sort based on the "major" version, followed by ...
crmprogdev's user avatar
3 votes
Accepted

Efficiently sorting a list of Work Order records by a Sequence

Consider mapping all your WorkOrders/Wrappers by a combination key (i.e UniqueKey__c + '.'+ Work_Order_Sequence__c.leftPad(10, '0') ). sort your combo key list comboKey = new List(mapWO.keyset()); ...
Gauge Boson's user avatar
3 votes
Accepted

How to sort list of dates in apex and return the entire record?

Your code is sorting the list you created in place by swapping values between pairs of indexes. So you should return listOfRecords. But the early code is wrong; it leaves iterationSize always zero. So ...
Keith C's user avatar
  • 136k

Only top scored, non community-wiki answers of a minimum length are eligible