function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
minkeshminkesh 

sorting Text field problem.

Hello,

           I have an requirement to sort Text field which contains (i.e. 0.0,0.1.0,0.0.0.1,1.0,1.1). it is the index of tree. i have another field which is Number field and it contains (0,1,2,3,...). i am making that above text field using this index field.

           now i have an requirement to display that visualforce page as an HTML page. and that Text field i need to put infront of tree node name.e.g.

                              1.0  a1

                                  1.1.0 a1.0

                                   1.1.1 a1.1

 

i am using that text  field so that it is sorting it by considering it String so my output becomes as follows which is wrong. can anyone help.

 



	
1   2.01.06.01 Australia
Role
Description
	
10   2.01.06.17 Spain
Role
Description
	
11   2.01.06.15 Portugal
Role
Description
	
12   2.01.06.19 United Kingdom & Ireland
Role
Description
	
13   2.01.06.02 Belgium
Role
Description
	
14   2.01.06.04 Czech Republic
Role
Description
	
16   2.01.06.10 India
Role
Description
	
17   2.01.06.12 Mexico
Role
Description
	
18   2.01.06.14 Poland
Role
Description
	
19   2.01.06.16 Slovenia
Role
Description
	
2   2.01.06.03 Canada
Role
Description
	
20   2.01.06.18 Switzerland
Role
Description
	
21   2.01.06.08 Hong Kong
Role
Description
	
3   2.01.06.05 France
Role
Description
	
4   2.01.06.07 Greece (Hellas
Role
Description
	
5   2.01.06.09 Hungary
Role
Description
	
6   2.01.06.11 Italy
Role
Description
	
7   2.01.06.13 Nordic & Baltic Region
Role
Description
	
8   2.01.06.06 Germany
Role
Description

 

Thank you,

Minkesh Patel



Best Answer chosen by Admin (Salesforce Developers) 
minkeshminkesh

Thank you for you reply. you are right i have to do it in my apex and i did it. i have used json String so as per the JSON string name i have done sorting. here is the code

jsStr --- json String
pActList -- list of actvity that need to sort


public List<ProcessActivity__c> jSonNodes(String jsStr, List<ProcessActivity__c> pActList) {
    	
    	System.debug('pActList: '+pActList.size());
    	Map<String, ProcessActivity__c> processActvityMap = new Map<String, ProcessActivity__c>();
    	for(ProcessActivity__c pa : pActList) {
    		processActvityMap.put(pa.Name.trim(), pa);
    	}
    	System.debug('processActvityMap: '+processActvityMap);
    	
    	//jsStr = '[{"data":"2.01.06.01 Australia","attr":{}},{"data":"2.01.06.03 Canada","attr":{}},{"data":"2.01.06.05 France","attr":{}},{"data":"2.01.06.07 Greece (Hellas","attr":{}},{"data":"2.01.06.09 Hungary","attr":{}},{"data":"2.01.06.11 Italy","attr":{}},{"data":"2.01.06.13 Nordic & Baltic  Region","attr":{}},{"data":"2.01.06.06 Germany","attr":{}},{"data":"2.01.06.17 Spain","attr":{}},{"data":"2.01.06.15 Portugal","attr":{}},{"data":"2.01.06.19 United  Kingdom &  Ireland","attr":{}},{"data":"2.01.06.02 Belgium","attr":{}},{"data":"2.01.06.04 Czech Republic","attr":{}},{"data":"2.01.06.10 India","attr":{}},{"data":"2.01.06.12 Mexico","attr":{}},{"data":"2.01.06.14 Poland","attr":{}},{"data":"2.01.06.16 Slovenia","attr":{}},{"data":"2.01.06.18 Switzerland","attr":{}},{"data":"2.01.06.08 Hong Kong","attr":{"class":" "}}]';
    	System.debug('jsStr: '+jsStr);
    	
    	List<String> nodeList = new List<String>();
    	List<String> jsStrSplit = jsStr.split('"data":"');
    	System.debug('jsStrSplit: '+jsStrSplit);
    	Integer listSize = jsStrSplit.size();
    	for(Integer i=1; i<listSize; i++) {
    		List<String> tempList = jsStrSplit.get(i).split(',');
    		
    		if(tempList.size() > 0) {
    			String temp = tempList.get(0);
    			temp = temp.substring(0, temp.length()-1);
	    		System.debug('temp:'+temp);
	    		nodeList.add(temp);
    		}
    		//nodeList.add(tempList[0].)
    	}
    	System.debug('nodeList: '+nodeList);
    	List<ProcessActivity__c> tempProcessActivityList = new List<ProcessActivity__c>();
    	for(String s : nodeList) {
    		System.debug('s: '+s);
    		String st = s.trim(); 
    		ProcessActivity__c pact = processActvityMap.get(st);
    		system.debug('-------------pact------------->'+pact);
    		if(pact != null) {
    			tempProcessActivityList.add(pact);
    		}
    	}
    	System.debug('tempProcessActivityList: '+tempProcessActivityList);
    	return tempProcessActivityList;
    }

 

 

All Answers

mtbclimbermtbclimber

You're going to need to manage this sorting yourself in Apex.

 

We don't have existing utilities that will help much other than list.sort() but that's only going to work for primitives so you'll need to manage the parsing.

minkeshminkesh

Thank you for you reply. you are right i have to do it in my apex and i did it. i have used json String so as per the JSON string name i have done sorting. here is the code

jsStr --- json String
pActList -- list of actvity that need to sort


public List<ProcessActivity__c> jSonNodes(String jsStr, List<ProcessActivity__c> pActList) {
    	
    	System.debug('pActList: '+pActList.size());
    	Map<String, ProcessActivity__c> processActvityMap = new Map<String, ProcessActivity__c>();
    	for(ProcessActivity__c pa : pActList) {
    		processActvityMap.put(pa.Name.trim(), pa);
    	}
    	System.debug('processActvityMap: '+processActvityMap);
    	
    	//jsStr = '[{"data":"2.01.06.01 Australia","attr":{}},{"data":"2.01.06.03 Canada","attr":{}},{"data":"2.01.06.05 France","attr":{}},{"data":"2.01.06.07 Greece (Hellas","attr":{}},{"data":"2.01.06.09 Hungary","attr":{}},{"data":"2.01.06.11 Italy","attr":{}},{"data":"2.01.06.13 Nordic & Baltic  Region","attr":{}},{"data":"2.01.06.06 Germany","attr":{}},{"data":"2.01.06.17 Spain","attr":{}},{"data":"2.01.06.15 Portugal","attr":{}},{"data":"2.01.06.19 United  Kingdom &  Ireland","attr":{}},{"data":"2.01.06.02 Belgium","attr":{}},{"data":"2.01.06.04 Czech Republic","attr":{}},{"data":"2.01.06.10 India","attr":{}},{"data":"2.01.06.12 Mexico","attr":{}},{"data":"2.01.06.14 Poland","attr":{}},{"data":"2.01.06.16 Slovenia","attr":{}},{"data":"2.01.06.18 Switzerland","attr":{}},{"data":"2.01.06.08 Hong Kong","attr":{"class":" "}}]';
    	System.debug('jsStr: '+jsStr);
    	
    	List<String> nodeList = new List<String>();
    	List<String> jsStrSplit = jsStr.split('"data":"');
    	System.debug('jsStrSplit: '+jsStrSplit);
    	Integer listSize = jsStrSplit.size();
    	for(Integer i=1; i<listSize; i++) {
    		List<String> tempList = jsStrSplit.get(i).split(',');
    		
    		if(tempList.size() > 0) {
    			String temp = tempList.get(0);
    			temp = temp.substring(0, temp.length()-1);
	    		System.debug('temp:'+temp);
	    		nodeList.add(temp);
    		}
    		//nodeList.add(tempList[0].)
    	}
    	System.debug('nodeList: '+nodeList);
    	List<ProcessActivity__c> tempProcessActivityList = new List<ProcessActivity__c>();
    	for(String s : nodeList) {
    		System.debug('s: '+s);
    		String st = s.trim(); 
    		ProcessActivity__c pact = processActvityMap.get(st);
    		system.debug('-------------pact------------->'+pact);
    		if(pact != null) {
    			tempProcessActivityList.add(pact);
    		}
    	}
    	System.debug('tempProcessActivityList: '+tempProcessActivityList);
    	return tempProcessActivityList;
    }

 

 

This was selected as the best answer
Adil_SFDCAdil_SFDC

Hi Minkesh.

 

I have similar issue. Please help me. I need to sort  the id in descending order.

When the documents come from integration the id is ascendingorder by default. How can i display it in descending order.

public void createDocuments(JSONParser parser)
{
documentList = new List<Document>();
Document doc = new Document();
Integer position = 0;
while (parser.nextToken() != null) 
{
if (parser.getCurrentToken() == JSONToken.START_OBJECT ||parser.getCurrentToken() ==JSONToken.END_OBJECT ) 

if(doc.title != null)// && doc.url != null && doc.rating != null && doc.filetype != null && doc.id != null && doc.description != null)
{
doc.position = position;
position++;
documentList.add(doc);

doc = new Document();


}
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) 
{
if(parser.getText() == 'title')
{
parser.nextToken();
doc.title = parser.getText();
}
if(parser.getText() == 'id')
{
parser.nextToken();
doc.id = parser.getText();

 

Pleas helo

minkeshminkesh

Hello,

 

I am not able to understand your question.

You mean salesforce id ?

 

Adil_SFDCAdil_SFDC
No it's Not Salesforce ID. ID is a string parsed from JSON. Sent from my iPhone 4S
minkeshminkesh
Actually in my case I was taking it from JSON. JSON is giving that numbers. See if there is any method in JSON class which helps you to do this. -- Thank you, Minkesh Patel 9374889004