+ Start a Discussion
Ajay Kumar 583Ajay Kumar 583 

Write the apex code to sort the n biggest numbers. input should be sent as a string with comma seperated values. the method should accept string of comma seperated numbers and a integer defining the value for n ?

Hi i need Apex code for this .
Best Answer chosen by Ajay Kumar 583
Deepali KulshresthaDeepali Kulshrestha
Hi Ajay,
Greetings to you!

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

    <aura:component controller="findBiggestValueClass" implements="flexipage:availableForAllPageTypes,force:appHostable" access="global">
        <aura:attribute name="stringToSort" type="String" access="global"/>
        <aura:attribute name="BiggestNumber" type="String" access="global"/>
        <lightning:input label="Enter String to find biggest number (Separate values by comma)" name="stringToSort" value="{!v.stringToSort}"/>
        <lightning:button variant="brand" label="Find Biggest Number" onclick="{!c.findBiggestInteger}"/>

        <div class="slds-p-top_medium">Entered Text : - {!v.stringToSort} </div>
        <div class="slds-p-top_medium">Biggest Value : - {!v.BiggestNumber} </div>
    </aura:component>
    
JS Controller : - 

    ({
        findBiggestInteger: function (c, e, h) {
            console.log('In findBiggestInteger');
            let action = c.get("c.sortSlctRec");
            action.setParams({
                "stringToSort" : c.get('v.stringToSort'),
                });
                action.setCallback(this, function (r) {
                if(r.getState() === 'SUCCESS') {
                    let storedResponse = r.getReturnValue();
                    console.log('storedResponse:');
                    console.log(storedResponse);
                    c.set('v.BiggestNumber',storedResponse);
                } else {
                    console.log('ERROR');
                    console.log(r.getError());
                }
            });
            $A.enqueueAction(action);
        }
    })

Apex Class(Controller) : - 

    public class findBiggestValueClass {

        @AuraEnabled
        public static Decimal sortSlctRec(String stringToSort) {
            try{
                String sortString = stringToSort;
                List<String> sortList = sortString.split(',');
                List<Decimal> intList = new List<Decimal>();
                for(String s : sortList){
                    intList.add(Decimal.valueOf(s));
                }
                intList.sort();
                System.debug('intList--->'+intList);
                System.debug('intList--->'+intList[intList.size()-1]);
                return intList[intList.size()-1];
            }catch (Exception ee){
                System.debug('Error : -'+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.

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Ajay,
Greetings to you!

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

    <aura:component controller="findBiggestValueClass" implements="flexipage:availableForAllPageTypes,force:appHostable" access="global">
        <aura:attribute name="stringToSort" type="String" access="global"/>
        <aura:attribute name="BiggestNumber" type="String" access="global"/>
        <lightning:input label="Enter String to find biggest number (Separate values by comma)" name="stringToSort" value="{!v.stringToSort}"/>
        <lightning:button variant="brand" label="Find Biggest Number" onclick="{!c.findBiggestInteger}"/>

        <div class="slds-p-top_medium">Entered Text : - {!v.stringToSort} </div>
        <div class="slds-p-top_medium">Biggest Value : - {!v.BiggestNumber} </div>
    </aura:component>
    
JS Controller : - 

    ({
        findBiggestInteger: function (c, e, h) {
            console.log('In findBiggestInteger');
            let action = c.get("c.sortSlctRec");
            action.setParams({
                "stringToSort" : c.get('v.stringToSort'),
                });
                action.setCallback(this, function (r) {
                if(r.getState() === 'SUCCESS') {
                    let storedResponse = r.getReturnValue();
                    console.log('storedResponse:');
                    console.log(storedResponse);
                    c.set('v.BiggestNumber',storedResponse);
                } else {
                    console.log('ERROR');
                    console.log(r.getError());
                }
            });
            $A.enqueueAction(action);
        }
    })

Apex Class(Controller) : - 

    public class findBiggestValueClass {

        @AuraEnabled
        public static Decimal sortSlctRec(String stringToSort) {
            try{
                String sortString = stringToSort;
                List<String> sortList = sortString.split(',');
                List<Decimal> intList = new List<Decimal>();
                for(String s : sortList){
                    intList.add(Decimal.valueOf(s));
                }
                intList.sort();
                System.debug('intList--->'+intList);
                System.debug('intList--->'+intList[intList.size()-1]);
                return intList[intList.size()-1];
            }catch (Exception ee){
                System.debug('Error : -'+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.
This was selected as the best answer
Ajay Kumar 583Ajay Kumar 583
Hi Deepali,

Thanks for replying. As i was new to Apex ,I needed only a simple Apex class and a method which will accept a string of numbers and returning the Biggest number. I have modified the above code to the simplest as below:

 public class findBiggestValueClass {

      public static integer sortSlctRec(String stringToSort)
      {

              String sortString = stringToSort;

              List<String> sortList = sortString.split(',');

              List<integer> intList = new List<integer>();

              for(String s : sortList){

                  intList.add(integer.valueOf(s));

              }

              intList.sort();

             System.debug('The given List is :'+intList);

              System.debug('The Biggest number is :'+intList[intList.size()-1]);

              return intList[intList.size()-1];
         }

        }
    
  Now i need  a integer defining the value for n ? Like for 
eg:  if  n = 2  display first two biggest number in the List.



Thanks,
Ajay