Hot answers tagged

14

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, Integer counter) { this.address = address; this.counter = counter; } public Integer compareTo(Object other) { return counter-((...


11

While Comparator isn't available out of the box, it's not terribly hard to implement. However, your performance will be considerably worse than a native solution, so you'll need to test and see how many rows you can actually sort before it becomes intolerable. public abstract class Comparator { public abstract Integer compare(Object o1, Object o2); ...


10

I would have to presume that the problem stems from the data appearing as an actual string to the sorting algorithm, thus producing lexicographical sorting (e.g. sorting as a string). You have plenty of choices to choose from if you want to introduce easy sorting: AngularJS has an orderBy directive, jQuery has a tableSort, module you can add into it (and ...


8

Problem is here: lstAttachments[iDx].LastModifiedDate.Date(). You are getting Date, which is not the same as DateTime (no time stored inside). In this case 2 first and 3 last records have the same date, which mean that they are sorted correctly. To fix that, lstAttachments[iDx].LastModifiedDate.Date(); needs to be replaced by lstAttachments[iDx]....


7

This line cause problem: selectedmeters = selectedmeters.sort(); Sort is void method, please check: http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_list.htm


7

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 reasonable approach to take (and how SOQL and many other systems handle it). If you want numerical ordering ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]), you'll ...


6

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 sorts sObjects in ascending order and compares sObjects using an ordered sequence of steps that specify the labels or fields used. The comparison starts with the ...


6

Java has multiple map classes such as LinkedHashMap that preserve insertion order but Apex only has one map type which does not. So in Apex a work-around is to keep a separate list of the keys as lists preserve insertion order and get the keys to iterate through from the list: private Map<string, groupingWrapClass> map_lineitems_Group = new ...


6

I have followed the steps in this link and I was able to fix the sorting on my apex:pageblocktable. It uses the jQuery tablesorter plugin to sort the table client-side. <apex:page standardController="Opportunity" tabStyle="Opportunity" extensions="myext" id="thepage"> <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.10....


6

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.


6

public class PartnerWrapper implements Comparable { private static final Map<String, Integer> levelsOrder = new Map<String, Integer>{ 'Platinum' => 1, 'Gold' => 2, 'Silver' => 3 }; public Account acc {get; set;} public String level {get; set;} public Integer compareTo(Object thatObj){ ...


6

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), new Version(8, 1, 3), ... }; you get sorting for free (as compareTo is implemented) where this sorts in place: src.sort(); and the string format for each ...


5

You can declare a constant list of mappings to numbers based on these values, eg: public class Wrapper implements Comparable{ public const colorMap Map<String,Integer> = new Map<String,Integer> {'Green'=>0, 'Yellow'=> '1','Red'=> '2'} .... rest of your class Then in the comparable method, you use the map to get numeric values ...


5

public class gAlertWrapper implements Comparable{ public Integer compareTo(Object compareTo) { gAlertWrapper gAlertWrapper = (gAlertWrapper) compareTo; if (gA.published__c == gAlertWrapper.gA.published__c) return 0; if (gA.published__c > gAlertWrapper.gA.published__c) return 1; return -1; } } Following is the correct syntax ...


5

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 > Rename Tabs and Labels, edit Product, and on the second screen, overwrite the Product Name field with the values Product Code and Product Codes, for the ...


5

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 work. From what I see, you don't absolutely require a Map, it was just the closest feature provided by Salesforce that you could find. Instead of a Map, I'd ...


5

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.sort(). Since your list is of String instances and Apex does not support custom sort functions, that would return your strings sorted in the manner that the ...


5

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 different instances of your ListWrapper, but the general pattern is going to be the same global Integer compareTo(Object o){ // typecast the argument so that you ...


5

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 are described in this answer. Basically, the collection becomes sorted in order to provide consistent iteration results across multiple iterations, even in ...


4

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. With very little logging enabled (just enough to debug results & profiling), results are now very consistently the same across all implementations. Thanks ...


4

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, Nominator_Code__c, Nominator_Type__c from Nominator__c WHERE FirstTwoLetters__c =: state and ...


4

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) The following factors affect results returned with ORDER BY: Sorting is case insensitive. ORDER BY is compatible with relationship query syntax. ...


4

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. There are some minor optimizations that could improve performance, such as not calling the Map.get method more than once per loop, sorting before putting them ...


4

In the sort method just set the sortBy conditionally. updateSort(event) { let fieldName = event.detail.fieldName; fieldName = fieldName === 'nameUrl' ? 'Name' : fieldName; let sortDirection = event.detail.sortDirection; this.sortBy = fieldName; this.sortDir = sortDirection; let reverse = sortDirection !== "asc"; let ...


4

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 calculate as you've attempted (unless you have some other need I'm not catching). var sortDirection = event.getParam('sortDirection'); component.set("v....


3

Follow the steps in this link to fix the sorting on apex:pageblocktable. It uses the jQuery tablesorter plugin to sort the table client-side. <apex:page standardController="Opportunity" tabStyle="Opportunity" extensions="myext" id="thepage"> <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"/> &...


3

I would write this code: Class A { list<WrapperB> listOfB {get;set;} class WrapperB implements Comparable { String name {get;set;} public WrapperC ref {get;set;} public Integer compareTo(Object o) { WrapperB that = (WrapperB) o; if (this.name < that.name) return -1; else if (...


3

Sid - you have many ordering issues all interplaying with each other Your SOQL query that places results in a set - by definition - has no order defined Your assignment of the set to a List of selectOptions; hence, also no order defined Chosen2 jQuery plugin that may or may not introduce a new sort order So, once the update back to the controller happens, ...


3

The value 0 is returned when the values are the same so for that case compare the other field: global Integer compareTo(Object objToCompare) { AreaWrapper that = (AreaWrapper) objToCompare; Integer c = this.AreaName.compareTo(that.AreaName); if (c != 0) { return c; } else { return this.ADName.compareTo(that.ADName); } }


3

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.


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