I see that the code where you find actual accounts is not returned in wrapper format
public pagereference Search()
{
String finalSearchValue = '%' + fullname + '%';
Accounts = [Select Id, Name, Active__c FROM Account
where Name like :finalSearchValue];
wraplist = new list<wrapData>();
for(Account a: Accounts)
{
wrapList.add(new wrapData(a));
}
return null;
}
Same way apply for filter method as well.
Here are a couple of general points on the subject...
Visualforce (and Apex) support map syntax so you can present in a basic way additional values established at runtime (e.g. obtained from describe calls or field sets or custom settings):
public class Wrapper {
...
public String[] keys {get; set;}
public Map<String, String> extras {get; set;}
...
public String[] contactFields {get; set;}
public Contact contact {get; set;}
...
}
<apex:repeat var"key" value="{!keys}">
<apex:inputText value="{!wrapper.extras[key]}"/>
</apex:repeat>
But to get the full benefit of the metadata behind an SObject field (such as data type including parsing/formatting, picklist values, label, help text) it is best to directly use the SObject fields (as SObject also supports map-like access):
<apex:repeat var"f" value="{!contactFields}">
<apex:inputField value="{!contact[f]}"/>
</apex:repeat>
And in Apex code there are string-based get/put methods available for any SObject too. So while you can't dynamically extend the wrapper class, you may be able to add code to it driven by data (such as lists of field names) that accomplishes the processing you require.
PS
To illustrate how code can work with a varying number of fields (including fields added by a user) this code will look at every field on a Contact object instance c
:
public void doSomething(Contact c) {
for (String f : Contact.SObjectType.getDescribe().fields.getMap().keySet()) {
Object o = c.get(f);
if (o instanceof String) ...
else if ...
}
}
and there is also API to get details of a field in a DescribeFieldResult.
Best Answer
There are multiple possibilities to sort a list of sObjects. The first and easiest one is a use of the
sort()
method of the List class. Next one is a bit tricky and it is aComparable interface
.List.sort()
If you have a list of sObjects you can use a standard method
sort()
.Assumed your list is a list of sObjects, it would be like this in that case:
It is easy to use but you have almost no way to controll the sort process.
Here you can read about sorting: Sorting Lists of sObjects
Comparable Interface
If you wish to have more flexible and powerful sort algorythm you can use a comparable interface. It adds a custom sort order to the list of sObjects (or other non-primitive types).
So the interface takes two list elements and compares based on some critera defined by you. And this is realy powerful. In the next example I'll use a field of the sObject as a comparison criteria.
Here an official doc:
Now it is up to you which option you will take. I would propose to implement an comparable interface. It is cool, powerfull and flexible.