Sort a list of sObjects: quick and dirty -or- elegant

2 Oct

Quick and Dirty

Here is a method that I’ve implemented to sort lists of objects by combinations of fields. I don’t claim that it is pretty, but it has worked for me.

public static list SortTrf (list iMyObjList) {
  list SortStringList = new list();
  list SortedObjList  = new list();
  map ObjMap = new map();
  MyObj__c TmpMyObj;
  String UniqueValue;
  for (MyObj__c m : iMyObjList) {
    UniqueValue = PadZeros(Integer.valueOf(m.SequencingField__c), 5) + m.Name;
    ObjMap.put(UniqueValue, m);
  for (String s : SortStringList) {
    TmpMyObj = ObjMap.get(s);
    if (TmpMyObj != null) SortedObjList.add(TmpMyObj);
  return SortedObjList;

With the help of a method from my other post.

public static String PadZeros(Integer Num, Integer Len) {
  String s = String.valueOf(Num);
  while (s.length() < Len) s = '0' + s;
  return s;


Implement the Comparable interface on a sub-class allows a list of the objects to be sorted using a familiar '.sort()' method.

public class MyObjWrap implements Comparable {
  public MyObj__c      MyObj       {get; set;}
  /* compareTo - sort using a field on MyObj__c */
  public Integer compareTo(Object compareTo) {
     ObjWrap TheObjWrap= (ObjWrap) compareTo;
     if (MyObj.MyField__c == TheObjWrap.MyObj.MyField__c) return 0;
     if (MyObj.MyField__c > TheObjWrap.MyObj.MyField__c) return 1;
     return -1;        

Now that your subclass is setup, you can use the built-in sort method for the instantiated object list.

list MyObjList = new list();
// load the list with your object

URL Parameters within Apex and Visualforce

28 Sep

Get a URL parameter from a Visualforce page

String MyParameter = ApexPages.currentPage().getParameters().get('urlparm');

Get a map of URL parameters from a Visualforce page

Map UrlParameterMap = ApexPages.currentPage().getParameters();
// check for a specific parameter
if (UrlParameterMap.containsKey('urlparm')){
  //do something with the parameter... UrlParameterMap.get('urlparm')

Get URL Parameters in a Visualforce page

This assumes the URL is looks like this, “”.

Put values into Visualforce page’s URL and load a new page

Method 1: write out the URL

public PageReference MyPageMethod() {
  String MyParameter = 'something';

  // redirect to a Visualforce page containing a url parm
  PageReference MyNewPage = new PageReference('/apex/newvfpage?urlparm=' + MyParameter);    
  // -or- redirect to a records page
  PageReference MyNewPage = new PageReference('/' + MyObj.Id);

  return MyNewPage;

Method 2: Put parameters into a PageReference

public PageReference MyPageMethod() {
  String MyParameter = 'something';

  PageReference MyNewPage = Page.PageTwo;

  return MyNewPage;

Put values into the current test page’s URL in a Test Class

private class MyControllerTest {
  static testMethod void myUnitTest() {
    // create a new page reference, set it as the current page, 
    // and use the ApexPages.currentPage() method to put the values
    PageReference PageRef = Page.MyPage;
    ApexPages.currentPage().getParameters().put('urlparm', 'theValue');      

    // load the controller and proceed with testing
    MyController controller = new MyController();

Salesforce Help

$CurrentPage Documentation
Controller Navigation Methods
Visualforce Parameters

Apex Loops, The Simple Things

18 Sep

For loop over a map’s keys (KeySet)

map MyAwesomeMap;
//... other code
for (String KeyStr: MyAwesomeMap.keySet()){

For loop over a map’s values

map MyAwesomeMap;
//... other code
for (CustomObject__c AnObject: MyAwesomeMap.values()){

For loop over a list

list MyAwesomeList;
//... other code
for (CustomObject__c AnObject: MyAwesomeList){

For loop over a set

set MyAwesomeSet;
//... other code
for (CustomObject__c AnObject: MyAwesomeSet){