Hot answers tagged

12

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-((...


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 ...


9

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); ...


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]....


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

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


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

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

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 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

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 ...


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

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.


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

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 ...


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

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

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.


3

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[key]}" var="caseInfo"> ...


3

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 API name of Due Date is ActivityDate. Also as suggested by @IllusiveBrian, here is the documentation on SOQL Order By - https://developer.salesforce.com/docs/...


3

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(element)


3

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 just an image and still have it sorted correctly. It has to do with the generated code that IMAGE produces. A better idea would be to use Static Resource files ...


3

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 the "minor" (for each major version), then each "finest" version (for the minor version). That will allow you to deal with the ones that don't have finer levels ...


2

Just pull the record directly off the wrapper public List<Electricity_Meter__c> getMeters() { OpportunityWrapper[] oppyList = new List<OpportunityWrapper>(); // do stuff oppyList.sort(); List<Electricity_Meter__c> selectedMeters = new List<Electricity_Meter__c>(); for (OpportunityWrapper wrapper : oppyList){ ...


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