+ Start a Discussion
nagalakshminagalakshmi 

How to sort the list as case insensitive

Hi,

 

How to sort the list  as case insensitive. I am used the method as

 

listname.sort();

 

It will sort the records. But it will display the records in sort order which are having caps lock as first letter. After that display's the records in sort order which are having small letters as first letter. Please help me how sort the list as case insensitivly.

 

Thanks,

Lakshmi.

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,


As you know Ascii value of ‘A’ is less than ‘a’. So either you have to convert all the string in lowercase or upper case before using the Sort method.

 

Try the below code as reference:

 


List<string> q1 = new string[3];
q1[0] = 'Test';
q1[1] = ‘test’;
q1[2] = 'XYZ';
for(integer i=0;i<q1.size();i++)
{
q1[i]=q1[i].toUpperCase();
}


q1.sort();

 


system.debug('@@@@@@@@@@@@@@' +q1[0]);
system.debug('@@@@@@@@@@@@@@' +q1[1]);
system.debug('@@@@@@@@@@@@@@' +q1[2]);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

kiranmutturukiranmutturu

use equalsIgnoreCase method..   

 

Returns true if the compString is not null and
represents the same sequence of characters as the String
that called the method, ignoring case. For example:
String myString1 = 'abcd';
String myString2 = 'ABCD';
equalsIgnoreCase String compString Boolean
Boolean result =
myString1.equalsIgnoreCase(myString2);
System.assertEquals(result, true);

Navatar_DbSupNavatar_DbSup

Hi,


As you know Ascii value of ‘A’ is less than ‘a’. So either you have to convert all the string in lowercase or upper case before using the Sort method.

 

Try the below code as reference:

 


List<string> q1 = new string[3];
q1[0] = 'Test';
q1[1] = ‘test’;
q1[2] = 'XYZ';
for(integer i=0;i<q1.size();i++)
{
q1[i]=q1[i].toUpperCase();
}


q1.sort();

 


system.debug('@@@@@@@@@@@@@@' +q1[0]);
system.debug('@@@@@@@@@@@@@@' +q1[1]);
system.debug('@@@@@@@@@@@@@@' +q1[2]);

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
nagalakshminagalakshmi

Hi Jain,

 

Thanks for your reply. It's working fine... But it will display's the list in caps which is having first letter as small letter. My requirement is want to sort the both caps and small letter words as case insensitive. And want to display list in sort order with small and caps letters. Please help me.. 

nagalakshminagalakshmi

Hi Jain, 

 

How to make the first letter as upper case for list of string values.

 

Thanks

Lakshmi

nagalakshminagalakshmi

Hi Jain,

 

I got the solution. Thanks

 

 for(integer i=0;i<tempList.size();i++)
{
tempList[i]=tempList[i].substring(0,1).toUpperCase()+tempList[i].substring(1,tempList[i].length()) + ' ';
}

tempList.sort();

 

Thanks,

Lakshmi

nagalakshminagalakshmi

Hi kiran,

 

How to sort upper and lower case string's as case insensitively. And display in sort order.

Like

 

Abcd

abcd

Bag

bag

.

.

.

Xyz

xyz

 

Please help me.

 

Thanks,

 

Lakshmi

Uwe HeimUwe Heim
public static string[] listSort(string[] input) {
  for(integer i=0; i < input.size(); i++) input[i] = input[i].toUpperCase() + input[i];
  input.sort();
  for(integer i=0; i < input.size(); i++) input[i] = input[i].substring( input[i].length() / 2 );
  return input;
}

clone the string uppercased as prefix for all elements. then sort. the remove the prefix again.