3 Replies Latest reply on Jun 27, 2019 7:33 AM by Greg Jason

    Sort checked row

    Greg Jason
      Share This:

      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. How to implement sort ? Any suggestions. Thank 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 {
        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;

      }

        • 1. Re: Sort checked row
          Greg Jason

          I found a solution, but it`s still not clear: the array is sorted only by the first digit and not by the whole number.

           

          @AuraEnabled

          public static List < ApexSort__c > sortSlctRec(List < String > slctRec) {

            List < ApexSort__c > sortRec = new List < ApexSort__c > ();

            accListClass alc = new accListClass();

          List <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;

                      //String stringExamp = '10';

                      //Integer integerExamp = integer.valueOf(stringExamp)

                      //system.assert(integerExample == 10)

                      sortRec.add(sortobj);

                  }   

                   update sortRec; 

                  sortRec.clear(); 

           

            } catch (Exception ex) {

             throw new AuraHandledException(ex.getMessage());

            }

            alc.accList = Database.query('SELECT Id, Unsorted__c, Sorted__c, Complexity__c, LastModifiedDate FROM ApexSort__c LIMIT 10');

            return alc.accList;

           

          }

           

          Any suggestions are welcome

          • 2. Re: Sort checked row
            Nick Rawlins

            Hi,

             

            You'll need to implement the comparable interface in Apex using the compareTo method...

             

            There is documentation and an example here:

             

            https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_comparable.htm

             

            Thanks,

             

            Nick

            2 of 2 people found this helpful
            • 3. Re: Sort checked row
              Greg Jason

              Thank you, Nick. I`m already solved it.

               

              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,',');

                         // if( string.join(intList,';')){

                         //     sortobj.Sorted__c = sortString;

                        //  }

                          sortobj.Sorted__c = sortString;

                          sortRec.add(sortobj);

                      }   

                      update sortRec; 

                      sortRec.clear(); 

              ...

               

              public class QuickSort  {

                private List<Integer> mNumbers;

                private Integer mNumber;

                public static void sortStatic(List<Integer> values){

                      (new Quicksort()).sort(values);

                }

                public void sort(List<Integer> values) {

                  if (values ==null || values.size()==0){

                    return;

                  }

                  this.mNumbers = values;

                  mNumber = values.size();

                  quicksort(0, mNumber - 1);

                }

                private void quicksort(Integer low, Integer high) {

                  Integer i = low, j = high;

                  Integer pivot = mNumbers[low + (high-low)/2];

                  while (i <= j) {

                    while (mNumbers[i] < pivot) {

                      i++;

                    }

                    while (mNumbers[j] > pivot) {

                      j--;

                    }

                    if (i <= j) {

                      exchange(i, j);

                      i++;

                      j--;

                    }

                  }

                  // Recursion

                  if (low < j)

                    quicksort(low, j);

                  if (i < high)

                    quicksort(i, high);

                }

                private void exchange(Integer i, Integer j) {

                  Integer temp = mNumbers[i];

                  mNumbers[i] = mNumbers[j];

                  mNumbers[j] = temp;

                }

              }

              1 of 1 people found this helpful