+ Start a Discussion
Eric TeseurEric Teseur 

Sort checked row

I have a tab where i save my values in Unsorted column. And when I check checkbox I want to copy value from Unsorted column then sort it and save in Sorted column
User-added image
Best Answer chosen by Eric Teseur
Ajay K DubediAjay K Dubedi
Hi Eric,

Use Below code it works properly for the whole number and a decimal number. 

@AuraEnabled
 public static void saveInput(List < ApexSort__c > ListApexSort) {
  Insert ListApexSort;
 }
 @AuraEnabled
 public List < ApexSort__c > accList = new List < ApexSort__c > ();

 @AuraEnabled
 public List < ApexSort__c > sortRec = new List < ApexSort__c > ();

 @AuraEnabled(cacheable = true)
 public static List < ApexSort__c > getArray(String query) {
  accListClass alc = new accListClass();
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c  order by LastModifiedDate desc LIMIT 10');
  return alc.accList;
 }

 @AuraEnabled
 public static List < ApexSort__c > sortSlctRec(List < String > slctRec) {
  List < ApexSort__c > sortRec = new List < ApexSort__c > ();
  accListClass alc = new accListClass();
 ApexSort__c sortApRec = [SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c WHERE Id IN: slctRec];
  try {
   
        for(ApexSort__c sortobj : sortApRec){
            String sortString = sortobj.Unsorted__c;
            List<String> sortList = sortString.split(',');
            List<Decimal> intList = new List<Decimal>();
            for(String s : sortList){
                intList.add(Decimal.valueOf(s));
            }
            intList.sort();
            sortString = string.join(intList,',');
            sortobj.Sorted__c = sortString;
            sortRec.add(sortobj);
        }    
        update sortRec;  
        
  } catch (Exception ex) {
   throw new AuraHandledException(ex.getMessage());
  }
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c LIMIT 10');
  return alc.accList;

 }
Thanks.

All Answers

Ajay K DubediAjay K Dubedi
Hi Eric,

You need to make a wrapper in javascript helper when you click chekbox then go to js heper method and get that particular raw record and updated(add sorted value) it by using wrapper. 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Eric TeseurEric Teseur
Thank you, Ajay. Can you post some example code? If you need I can post mine
Deepali KulshresthaDeepali Kulshrestha
Hi Eric,
Greetings to you!

- I read your problem and implemented in my Org.
- Please use the below code [Solved] : -

Component : - 

  
<aura:component access="global" controller="sortDataClass" implements="forceCommunity:availableForAllPageTypes,flexipage:availableForAllPageTypes">
        <aura:attribute name="data" type="Object"/>
        <aura:attribute name="columns" type="List"/>
        <aura:attribute name="selectedRows" type="List" />
        <aura:attribute name="selectedRowsCount" type="Integer" default="0"/>
        <aura:attribute name="selectedData" type="Object[]" default="[]" access="global" />
        <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
        <lightning:datatable
                title="DATA TABLE"
                columns="{! v.columns }"
                data="{! v.data }"
                keyField="id"
                selectedRows="{! v.selectedRows }"
                onrowselection="{! c.updateSelectedText }"
        />
    </aura:component>



Controller JS : -

  
({

        doInit : function (c, e, h){
            h.doInit_helper(c, e, h);
        },

        updateSelectedText : function(c, e, h){
            console.log('In updateSelectedText');
            h.updateSelectedText_helper(c, e, h);
        },
    })


    
Helper JS : - 
      
({
            doInit_helper : function(c, e, h) {
                try{
                    console.log('doInit_helper');

                    let action = c.get("c.tableRecord");
                    action.setCallback(this, function(r) {
                        console.log(r.getState());
                        if (r.getState() === 'SUCCESS') {
                            let storedResponse = r.getReturnValue();
                            console.log('<--storedResponse1o1-->');
                            console.log('storedResponse',storedResponse);

                            c.set("v.columns", [
                                  { label: 'Account Name', fieldName: 'Name', type: 'Text' },
                                  { label: 'Unsorted Data', fieldName: 'unsorted_data__c', type: 'Text' },
                                  { label: 'Sorted Data', fieldName: 'sorted_data__c', type: 'Text' },
                            ]);
                            if (!$A.util.isUndefinedOrNull(storedResponse) && !$A.util.isEmpty(storedResponse)) {
                                    c.set("v.data",storedResponse);
                            }
                        }
                    });
                    $A.enqueueAction(action);
                } catch (e){
                    console.log('Error -->>'+e);
                }
            },

            updateSelectedText_helper: function(c, e, h){
                try{
                    console.log('updateSelectedText');
                      let selectedRows = e.getParam('selectedRows');
                      //  console.log('selectedRows'+selectedRows);
                      console.log('selectedRows type--->'+typeof(selectedRows));
                       selectedRows.forEach(function(el){
                         if(!$A.util.isUndefinedOrNull(el.unsorted_data__c) && !$A.util.isEmpty(el.unsorted_data__c)){
                           console.log('el>>'+JSON.stringify(el));
                           console.log('el>>'+el.unsorted_data__c);
                           let str = el.unsorted_data__c;
                           console.log('str-->'+str);
                            var b = str.split(',').map(function(item) {
                              return parseInt(item, 10);
                          });
                           console.log('b--->'+b);
                         console.log('type b--->'+typeof(b));

                            b.sort(function (a, c) {
                                return (a-c);
                            });
                             console.log('b--->'+b);
                             console.log('type b--->'+typeof(b));
                            let str2 = b.join(',');
                            console.log('str2-->'+str2);
                            console.log('typeof str2-->'+typeof(str2));
                            let action = c.get("c.saveSortedData");
                            action.setParams({
                                "str2" : str2,
                                "Ids" : el.Id
                            });
                            action.setCallback(this, function(r) {
                                console.log(r.getState());
                                if (r.getState() === 'SUCCESS') {
                                    let storedResponse = r.getReturnValue();
                                    console.log('<--storedResponse1o1-->');
                                    console.log('storedResponse',storedResponse);
                                    if (!$A.util.isUndefinedOrNull(storedResponse) && !$A.util.isEmpty(storedResponse)) {
                                            console.log(storedResponse);
                                    }
                                }
                            });
                            $A.enqueueAction(action);
                            h.doInit_helper(c, e, h);
                        }
                    });
                } catch (e){
                    console.log('Error--->'+e);
                }
            },
        })


        
Class : - 
  
public class sortDataClass {

        @AuraEnabled
        public static List<Account> tableRecord(){
            try{
                System.debug('In tableRecord');
                List<Account> accList = new List<Account>();
                accList = [SELECT Name,Id,unsorted_data__c,sorted_data__c FROM Account WHERE unsorted_data__c!=null LIMIT 1000];
                System.debug('accList-->'+accList);
                return accList;
            } catch (Exception ee){
                System.debug('Error msg --> '+ee.getMessage()+'   Line Number -->'+ee.getLineNumber());
            }
            return null;
        }

        @AuraEnabled
        public static List<Account> saveSortedData(String str2,String Ids){
            try{
                System.debug('In saveSortedData');
                List<Account> newaccList = new List<Account>();
                Account acccObj = new Account();
                acccObj.Id = Ids;
                acccObj.sorted_data__c = str2;
                newaccList.add(acccObj);
                upsert  newaccList;
                return newaccList;
            } catch (Exception ee){
                System.debug('Error msg --> '+ee.getMessage()+'   Line Number -->'+ee.getLineNumber());
            }
            return null;
        }
    }


    
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
 
Eric TeseurEric Teseur

Hi, Deepali

Thanks for your answer, all are working fine. But i want solve it with apex side, here is my code:

 

@AuraEnabled
 public static void saveInput(List < ApexSort__c > ListApexSort) {
  Insert ListApexSort;
 }
 @AuraEnabled
 public List < ApexSort__c > accList = new List < ApexSort__c > ();

 @AuraEnabled
 public List < ApexSort__c > sortRec = new List < ApexSort__c > ();

 @AuraEnabled(cacheable = true)
 public static List < ApexSort__c > getArray(String query) {
  accListClass alc = new accListClass();
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c  order by LastModifiedDate desc LIMIT 10');
  return alc.accList;
 }

 @AuraEnabled
 public static List < ApexSort__c > sortSlctRec(List < String > slctRec) {
  List < ApexSort__c > sortRec = new List < ApexSort__c > ();
  accListClass alc = new accListClass();
 ApexSort__c sortApRec = [SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c WHERE Id IN: slctRec];
  try {
      sortApRec.Sorted__c = sortApRec.Unsorted__c; 
      sortRec.add(sortApRec);

    if ( sortRec.size() >1  ) {  
         sortRec.sort();  
        //Quicksort.sortStatic(sortRec);

         update sortRec;  
        sortRec.clear(); 
        //sortRec.sort();
    }  
         if ( sortRec.size() > 0 ) { 
       // sortRec.sort();     
        update sortRec;  
        sortRec.clear();    
    }  

  } catch (Exception ex) {
   throw new AuraHandledException(ex.getMessage());
  }
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c LIMIT 10');
  return alc.accList;

 }
How to implement sort of sortApRec ? (built-in sorting apex functions doesn`t work) Any suggestions?
Ajay K DubediAjay K Dubedi
Hi Eric,

Use below code it may helpful for you
 
@AuraEnabled
 public static void saveInput(List < ApexSort__c > ListApexSort) {
  Insert ListApexSort;
 }
 @AuraEnabled
 public List < ApexSort__c > accList = new List < ApexSort__c > ();

 @AuraEnabled
 public List < ApexSort__c > sortRec = new List < ApexSort__c > ();

 @AuraEnabled(cacheable = true)
 public static List < ApexSort__c > getArray(String query) {
  accListClass alc = new accListClass();
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c  order by LastModifiedDate desc LIMIT 10');
  return alc.accList;
 }

 @AuraEnabled
 public static List < ApexSort__c > sortSlctRec(List < String > slctRec) {
  List < ApexSort__c > sortRec = new List < ApexSort__c > ();
  accListClass alc = new accListClass();
 ApexSort__c sortApRec = [SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c WHERE Id IN: slctRec];
  try {
   
        for(ApexSort__c sortobj : sortApRec){
            String sortString = sortobj.Unsorted__c;
            List<String> sortList = sortString.split(',');
            sortList.sort();
            sortString = string.join(sortList,',');
            sortobj.Sorted__c = sortString;
            sortRec.add(sortobj);
        }    
        update sortRec;  

  } catch (Exception ex) {
   throw new AuraHandledException(ex.getMessage());
  }
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c LIMIT 10');
  return alc.accList;

 }
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Ajay K DubediAjay K Dubedi
Hi Eric,

Use Below code it works properly for the whole number and a decimal number. 

@AuraEnabled
 public static void saveInput(List < ApexSort__c > ListApexSort) {
  Insert ListApexSort;
 }
 @AuraEnabled
 public List < ApexSort__c > accList = new List < ApexSort__c > ();

 @AuraEnabled
 public List < ApexSort__c > sortRec = new List < ApexSort__c > ();

 @AuraEnabled(cacheable = true)
 public static List < ApexSort__c > getArray(String query) {
  accListClass alc = new accListClass();
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c  order by LastModifiedDate desc LIMIT 10');
  return alc.accList;
 }

 @AuraEnabled
 public static List < ApexSort__c > sortSlctRec(List < String > slctRec) {
  List < ApexSort__c > sortRec = new List < ApexSort__c > ();
  accListClass alc = new accListClass();
 ApexSort__c sortApRec = [SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c WHERE Id IN: slctRec];
  try {
   
        for(ApexSort__c sortobj : sortApRec){
            String sortString = sortobj.Unsorted__c;
            List<String> sortList = sortString.split(',');
            List<Decimal> intList = new List<Decimal>();
            for(String s : sortList){
                intList.add(Decimal.valueOf(s));
            }
            intList.sort();
            sortString = string.join(intList,',');
            sortobj.Sorted__c = sortString;
            sortRec.add(sortobj);
        }    
        update sortRec;  
        
  } catch (Exception ex) {
   throw new AuraHandledException(ex.getMessage());
  }
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c LIMIT 10');
  return alc.accList;

 }
Thanks.
This was selected as the best answer
Eric TeseurEric Teseur

Hi Ajay

Thank you, all is working awesome. With your help I`m solved like this:
 

try {
   
        for(ApexSort__c sortobj : sortApRec){
            String sortString = sortobj.Unsorted__c;
            List<String> sortList = sortString.split(',');
            List<Integer> intList = new List<Integer>();
            for(String s : sortList){
                intList.add(Integer.valueOf(s));
            }
          QuickSort.sortStatic(intList);
            sortString = string.join(intList,',');
            sortobj.Sorted__c = sortString;
            sortRec.add(sortobj);
        }    
        update sortRec;
Deepali KulshresthaDeepali Kulshrestha
Hi Eric,
Greetings to you!

- I read your problem and implemented in my Org.
- Please use the below code [Solved] : -
 
Class : - 

    @AuraEnabled
 public static void saveInput(List < ApexSort__c > ListApexSort) {
  Insert ListApexSort;
 }
 @AuraEnabled
 public List < ApexSort__c > accList = new List < ApexSort__c > ();

 @AuraEnabled
 public List < ApexSort__c > sortRec = new List < ApexSort__c > ();

 @AuraEnabled(cacheable = true)
 public static List < ApexSort__c > getArray(String query) {
  accListClass alc = new accListClass();
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c  order by LastModifiedDate desc LIMIT 10');
  return alc.accList;
 }

 @AuraEnabled
 public static List < ApexSort__c > sortSlctRec(List < String > slctRec) {
  List < ApexSort__c > sortRec = new List < ApexSort__c > ();
  accListClass alc = new accListClass();
 ApexSort__c sortApRec = [SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c WHERE Id IN: slctRec];
  try {
        for(ApexSort__c sortobj : sortApRec){
            String sortString = sortobj.Unsorted__c;
            List<String> sortList = sortString.split(',');
            List<Integer> covertStringtoNumberList= new List<Integer>();
            for(String inst: sortList){
                Integer objInt= Integer.valueOf(inst);
                covertStringtoNumberList.add(objInt);
            }
            covertStringtoNumberList.sort();
            sortString = string.join(covertStringtoNumberList,',');
            System.debug('sortString-->'+sortString);
            sortobj.Sorted__c = sortString;
            sortRec.add(sortobj);
        }    
        update sortRec;  
  } catch (Exception ex) {
   throw new AuraHandledException(ex.getMessage());
  }
  alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c FROM ApexSort__c LIMIT 10');
  return alc.accList;

 }


    
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.