Liquid UI - Documentation - 15.07 Sort two dimensional array values in different ways

15.07 Sort two dimensional array values in different ways


Prerequisites


Purpose

To sort values in a two-dimensional array based on the first and second elements using various sorting methods. The elements in the array can also be sorted as needed using a pushbutton, which will call the required sorting method.

Different sorting methods are written in the Liquid UI script such as Ascending order SORT for the first element, Ascending order SORT for the second element, Ascending order SORT for both the first and second element, Descending order SORT for the first element, Descending order SORT for the second element, Descending order SORT for both the first and second element.


User Interface

//Create this file inside your script folder for adding functionality to customize the SAP Easy Access Screen: SAPLSMTR_NAVIGATION.E0100

//Now, let's start adding the Liquid UI script to the above file and save it.

  1. Add the following Liquid UI script to the above file and save it.
     
    // Function for Ascending order SORT for First element
    function myFunctionFirstAscend() {
       myarr.sort(function(a,b)
          {return a[0] < b[0] ? -1 : 1}
       );
    }
    
    // Function for Ascending order SORT for Second element
    function myFunctionSecondAscend() {
       myarr.sort(function(a,b)
          {return a[1] < b[1] ? -1 : 1}
       );
    }
    
    // Function to for Ascending order SORT both first and Second element
    function myFunctionAscendSort() {
       myarr.sort(function(a,b)
          {return a[1] == b[1] ? (a[0] < b[0] ? -1 : 1) : (a[1] < b[1] ? -1 : 1)}
       );
    }
    
    // Function for Descending order SORT for First element
    function myFunctionFirstDescend() {
       myarr.sort(function(a,b)
          {return a[0] > b[0] ? -1 : 1}
       );
    }
    
    // Function for Descending order SORT for Second element
    function myFunctionSecondDescend() {
       myarr.sort(function(a,b)
          {return a[1] > b[1] ? -1 : 1}
       );
    }
    
    // Function for Descending order SORT both first and Second element
    function myFunctionDescendSort() {
       myarr.sort(function(a,b)
          {return a[1] == b[1] ? (a[0] > b[0] ? -1 : 1) : (a[1] > b[1] ? -1 : 1)}
       );
    }
    
    var myarr = [
      [57326, 'GULL-1'],
      [60791, 'GULL-3'],
      [58778, 'GULL-3'],
      [60455, 'GULL-1'],
      [60603, 'GULL-3'],
      [52374, 'GULL-3'],
      [60245, 'GULL-1']
    ];
    
    // Array
    println("\n Array\n ");
    for(j=0;j<myarr.length;j++)
    println(myarr[j]);
    // sort
    println("\nSorted Array\n");
    myFunctionDescendSort();
    for(j=0;j<myarr.length;j++)
    println(myarr[j]);
    

 

SAP Process

  1. Logon to SAP. Now, you can find the sorted values in the Console window. Here, you can see the array values are sorted based on the function written for Descending order SORT for both the first and second element (myFunctionDescendSort()), as shown in the image below.
     

Can't find the answers you're looking for?