Author Topic: Two Dimensional Array Sort  (Read 5158 times)

Rahul Gera

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 88
    • View Profile
Two Dimensional Array Sort
« on: September 21, 2018, 01:42:05 PM »
Purpose: To sort a two dimensional array based on the first & second element.

Liquid UI Code:

// 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 to 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<arr.length;j++)
println(arr[j]);
// sort
println("\nSorted Array\n");
myFunction<userdefined>();
for(j=0;j<myarr.length;j++)
println(myarr[j]);
« Last Edit: September 21, 2018, 02:15:02 PM by Rahul Gera »