function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shri RajShri Raj 

List of Integers in Ascending/Descending order

I always get confused with indexing. I would like to print these numbers in Ascending & Descending order. Can someone help. 

List<Integer> intergerList = new List<Integer> {33,2,45,343,433,22,111} ;

 

Best Answer chosen by Shri Raj
Shri RajShri Raj
Thanks Denis. Actually this is what i was looking for . Using Indexing

// DESCENDING ORDER
List<Integer> integerList = new List<integer>{4,5,5,443,3,2} ;

for(Integer i = 0 ; i < integerList.size() ; i ++) {
for(integer j = i+1 ; j <= integerList.size() -1 ; j ++ ){
  integer x = 0 ;
  if(integerList[i] <  integerList[j]){
   x = integerList[i]  ;
   integerList[i] = integerList[j]  ;
   integerList[j]  = x;
   system.debug(integerList) ;
  }
}
}

// ASCENDING ORDER
List<Integer> integerList = new List<integer>{4,5,5,443,3,2} ;

for(Integer i = 0 ; i < integerList.size() ; i ++) {
for(integer j = i+1 ; j <= integerList.size() -1 ; j ++ ){
  integer x = 0 ;
  if(integerList[i] >  integerList[j]){
   x = integerList[i]  ;
   integerList[i] = integerList[j]  ;
   integerList[j]  = x;
   system.debug(integerList) ;
  }
}
}

All Answers

Denis VakulishinDenis Vakulishin
Hi,
List has sort() method. It sorts item in in ascending order. https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_list.htm#apex_System_List_sort
To get descending order just reverse the list manualy(Maybe there's built-in solution but I don't know about it).
List<Integer> sampleList =new List<Integer>();
sampleList.add(5);
sampleList.add(2);
sampleList.add(10);
sampleList.sort();  // This function always results in asc order.


List<Integer> finalList = new List<Integer>();
for(Integer i = sampleList.size()-1; i>=0;i--)
{
    finalList.add(sampleList.get(i));
}


Shri RajShri Raj
Thanks Denis. Actually this is what i was looking for . Using Indexing

// DESCENDING ORDER
List<Integer> integerList = new List<integer>{4,5,5,443,3,2} ;

for(Integer i = 0 ; i < integerList.size() ; i ++) {
for(integer j = i+1 ; j <= integerList.size() -1 ; j ++ ){
  integer x = 0 ;
  if(integerList[i] <  integerList[j]){
   x = integerList[i]  ;
   integerList[i] = integerList[j]  ;
   integerList[j]  = x;
   system.debug(integerList) ;
  }
}
}

// ASCENDING ORDER
List<Integer> integerList = new List<integer>{4,5,5,443,3,2} ;

for(Integer i = 0 ; i < integerList.size() ; i ++) {
for(integer j = i+1 ; j <= integerList.size() -1 ; j ++ ){
  integer x = 0 ;
  if(integerList[i] >  integerList[j]){
   x = integerList[i]  ;
   integerList[i] = integerList[j]  ;
   integerList[j]  = x;
   system.debug(integerList) ;
  }
}
}
This was selected as the best answer
Denis VakulishinDenis Vakulishin
Hi,
Here's implementation of QSort algorythm. It's sorting in ascending order
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) {
    // check for empty or null array
    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;
    // Get the pivot element from the middle of the list
    Integer pivot = mNumbers[low + (high-low)/2];

    // Divide into two lists
    while (i <= j) {
      // If the current value from the left list is smaller then the pivot
      // element then get the next element from the left list
      while (mNumbers[i] < pivot) {
        i++;
      }
      // If the current value from the right list is larger then the pivot
      // element then get the next element from the right list
      while (mNumbers[j] > pivot) {
        j--;
      }

      // If we have found a values in the left list which is larger then
      // the pivot element and if we have found a value in the right list
      // which is smaller then the pivot element then we exchange the
      // values.
      // As we are done we can increase i and 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;
  }
}

Usage 
List<Integer> integerList = new List<integer>{4,5,5,443,3,2} ;
Quicksort.sortStatic(integerList);
System.debug(integerList);

And this is much faster than yours buble sort. But if List is small it realy doesn't matter.