Code Monkey home page Code Monkey logo

apex-sort-sobs's Introduction

apex-sort-sobs

Utility class that provides a short-hand method for sorting of Salesforce SObjects by any field. Uses a Quicksort implementation with Dynamic Type Inference.

usage

Sort on Field:

Account[] accs = [SELECT My_Custom_Field__c FROM Account LIMIT 2000];
SortSobs.ascending(accs, Account.My_Custom_Field__c);

Sort in reverse order (desc)

Account[] accs = [SELECT My_Custom_Field__c FROM Account LIMIT 2000];
SortSobs.descending(accs, Account.My_Custom_Field__c);

Sort on parent field:

List<Contact> entries = [SELECT Name, Account.Name FROM Contact LIMIT 2000];
SortSobs.ascending(entries, SObjectField[]{ Contact.Account, Account.Name });

When sorting on parent fields, the last item in the SObjectField array is always the field to sort on. All previous items must be relationship fields!

considerations

This implemenation is a little slower than a Comparable, mostly because of overhead in Type casting and we have to run an intial iteration to capture the values to sort.

todo

  • Optimization around casting/general overhead
  • Provide more complete benchmarking

benchmarking

n=2000

  • built in List.sort() method 0.017s
  • w/ custom comparable wrapper: 0.377s
  • Hardcoded Quicksort: 0.435s
  • Dynamic Field Quicksort: 0.692s
  • Dynamic Field Quicksort on relationship (2 levels): 0.692s

Benchmark code for custom comparable wrapper for reference:

  public class ComparatorTest implements Comparable {
    public Account te;
    public ComparatorTest(Account te){
        this.te = te;
    }

    // Implement the compareTo() method
    public Integer compareTo(Object compareTo) {
        ComparatorTest compareToEmp = (ComparatorTest)compareTo;
        if (this.te.Name == compareToEmp.te.Name) return 0;
        if (this.te.Name > compareToEmp.te.Name) return 1;
        return -1;
    }
   }

License

MIT

apex-sort-sobs's People

Contributors

chuckjonas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.