I want to sort based on the number in a string field which also contains special character for example : (▲45) 887 i want to sort based on the part 887

Hello,

I have attached the image and I want to sort on the 4th column namely Rcvrd which has some non-numerical values and want to sort for the values after the end of the brackets. It would be great if you could help me.

Thank you

Attachment: grid_62e80243.zip

4 Replies

SK Sujith Kumar Rajkumar Syncfusion Team May 11, 2020 10:31 AM UTC

Hi Sagar, 
 
Greetings from Syncfusion support. 
 
From your query we could see that your requirement is to customize the sort action for a particular column. You can achieve this using columns sortComparer function where you can customize the default sort action. This is explained for the CustomerID column in the below code snippet, 
 
app.component.html 
<ejs-grid #grid [dataSource]='data' [allowSorting]='true'> 
    <e-columns> 
        <e-column field='Freight' [sortComparer]='sortComparer' headerText='Freight' width='120' textAlign='Right'></e-column> 
    </e-columns> 
</ejs-grid> 
 
app.component.ts 
// Sort comparer function 
sortComparer(reference: string, comparer: string) { 
      // This will be triggered for comparing each value with the reference value 
      // This can be customized to compare the values as per your need and return the corresponding result 
      if (reference < comparer) { 
          return -1; 
      } 
      if (reference > comparer) { 
          return 1; 
      } 
      return 0; 
} 
 
More details on this can be found in the below help documentation link, 
 
 
 
Please get back to us if you require any further assistance. 
 
Regards, 
Sujith R 



SM Sagar Motwani May 29, 2020 05:15 PM UTC

Hi Sujith,

Thanks for replying. When I try to do the following it gives me an error of reference.indexOf is not a function.

public sortComparer = (reference: string, comparer: string)=> {
if (reference.indexOf(")"))
{
console.log(reference)
}
else
{
reference=reference
}
if (reference < comparer) {


console.log(reference)
console.log(comparer)
return -1;
}
if (reference > comparer) {

console.log(reference)
console.log(comparer)
return 1;
}
return 0;
}


SM Sagar Motwani May 29, 2020 05:23 PM UTC

Some sample data is also attached

Attachment: sample_71d6ef62.zip


SK Sujith Kumar Rajkumar Syncfusion Team June 1, 2020 12:08 PM UTC

Hi Sagar, 

Your reported problem occurs if the ‘Rcvrd’ column that you have bound in the Grid is a number type column for which you cannot access the indexOf method. For this case you need to convert the number to string and then check your condition. So please ensure the value checked is in the proper format for your case. 

Sample for your reference, 


If you are still facing problem please let us know what value is received for ‘reference’ and ‘comparer’ arguments in the columns sortComparer method when the error is thrown based on which we will provide the further details. 

Let us know if you have any concerns. 

Regards, 
Sujith R 


Loader.
Up arrow icon