-1
public with sharing class TableData {

    public String Id { get; set; } // hash modified 
    public Decimal duration { get; set; }
    public String description { get; set; }
    public String name { get; set; }
    public String owner { get; set; }
    public String type { get; set; }
    public Decimal timeAllocated { get; set; }
    public String dueDate { get; set; }
    public String status { get; set; }



        public TableData(String id, Decimal duration, String description, String name, String owner, String type,
                            Decimal timeAllocated, String dueDate, String status) {

            this.id = id; // hash modified
            this.duration = duration;
            this.description = description;
            this.name = name;
            this.owner = owner;
            this.type = type;
            this.timeAllocated = timeAllocated;
            this.dueDate = dueDate;
            this.status = status;

        }

}
1

You would sort them before serialization. That is the raison d'être for the Comparable interface. If, for instance, you wanted to sort by dueDate it would look something like:

class MyWrapper implements Comparable
{
    public Date dueDate { get; private set; }
    // other properties
    public MyWrapper(...)
    {
        // constructor
    }
    public Integer compareTo(Object instance)
    {
        MyWrapper that = (MyWrapper)instance;

        // nulls last
        if (that.dueDate == null) return 1;
        if (this.dueDate == null) return -1;

        // descending
        return (this.dueDate > that.dueDate) ? -1 : 1;
    }

    public static String serializeSorted(List<MyWrapper> input)
    {
        // you could add a convenience method which combines steps
        input.sort();
        return JSON.serialize(input);
    }
}
  • I figured out what i was trying to say lol. The properties within the wrapper are sorted automatically and cannot be ordered with serialize. The order of the objects themselves is retained if the collection is an ordered collection – Eric May 13 '17 at 2:54
  • @Eric Ah, right, they're serialized in the order they're declared? – Adrian Larson May 13 '17 at 2:56
  • I do not believe so. They are sorted by the JSON in Alpha (reverse) order. At least in the few quick anon examples I just did. By no means concrete though. I wonder if that is what the OP was asking about given the words "json key" in the comments (since deleted) – Eric May 13 '17 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.