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
mdinatalemdinatale 

list sorting help

I'am able to sort my list with .sort() it is a string field but the results are

 

1

2

3

4

41

43

44

5

TTO

 

I need it to be

1

2

3

4

5

41

43

44

TTO

 

Any help would be greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
vriavmvriavm

Hi,

       Please use below logic. I tested and it works fine.

List<String> stringLst = new List<String>();
stringLst.add('1');
stringLst.add('2');
stringLst.add('3');
stringLst.add('4');
stringLst.add('41');
stringLst.add('43');
stringLst.add('44');
stringLst.add('5');
stringLst.add('TTO');
List<String> newStringLst = new List<String>();
List<Integer> newIntegerLst = new List<Integer>();
Integer temp = null;
for (Integer i =0; i < stringLst.size() ; i ++) {
    System.debug('stringLst' + i + ' : ' + stringLst[i]);
    try {
        temp = Integer.ValueOf(stringLst[i]);
        newIntegerLst.add(temp);
    } catch (Exception e) {
        newStringLst.add(stringLst[i]);
    }
}
system.debug('-------------------------------------');
newIntegerLst.sort();
newStringLst.sort();
stringLst.clear();
for (Integer i =0; i < newIntegerLst.size() ; i ++) {
    stringLst.add(String.ValueOf(newIntegerLst[i]));
}
stringLst.addall(newStringLst);
for (Integer i =0; i < stringLst.size() ; i ++) {
    System.debug('stringLst' + i + ' : ' + stringLst[i]);
}

Thanks & Regards,

Vivek V.

All Answers

vriavmvriavm

Hi,

       Please use below logic. I tested and it works fine.

List<String> stringLst = new List<String>();
stringLst.add('1');
stringLst.add('2');
stringLst.add('3');
stringLst.add('4');
stringLst.add('41');
stringLst.add('43');
stringLst.add('44');
stringLst.add('5');
stringLst.add('TTO');
List<String> newStringLst = new List<String>();
List<Integer> newIntegerLst = new List<Integer>();
Integer temp = null;
for (Integer i =0; i < stringLst.size() ; i ++) {
    System.debug('stringLst' + i + ' : ' + stringLst[i]);
    try {
        temp = Integer.ValueOf(stringLst[i]);
        newIntegerLst.add(temp);
    } catch (Exception e) {
        newStringLst.add(stringLst[i]);
    }
}
system.debug('-------------------------------------');
newIntegerLst.sort();
newStringLst.sort();
stringLst.clear();
for (Integer i =0; i < newIntegerLst.size() ; i ++) {
    stringLst.add(String.ValueOf(newIntegerLst[i]));
}
stringLst.addall(newStringLst);
for (Integer i =0; i < stringLst.size() ; i ++) {
    System.debug('stringLst' + i + ' : ' + stringLst[i]);
}

Thanks & Regards,

Vivek V.

This was selected as the best answer
Adil_SFDCAdil_SFDC

I need a little help here.  I am displaying documents from a website on VF Page. The order it is displayed is ascending. 

I want to display it in descending order. I am using sort() method. 

I get following error 

Method does not exist or incorrect signature: [String].sort()

Here is my code

 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();
                   system.debug('DDDDD'+doc.id);
                   String sortid = doc.id.sort();
                    docIdsList.add(doc.id);
                    
                }