Google Script sort 2D Array by any column -


I was asked a question before getting a record from a database:

I tampered the array It is quite easy to do and create your own sorting algorithm, but I want to use the current Array.sort () method to organize the data due to its speed. I think I can easily use it to sort the 2D array through the first column of data, but I can not get the syntax for sorting on the different columns of data other than the first data

The closest I have found is:. However, these inputs have not worked for my Array, TableData:

  for tableData.sort ([[column: 1}], I get this for the following code; Type = error: (square) @ 4dde8e64 function, this is an object. (Line 49, file "sortableball")  
  TableData.sort ([[Column: 1, ascending: true]]);   

= & gt; TypeError: (class) @ 4d89c26e is not a function, this is an object (line 50, file "Sortableball")

Which column of data to choose What is the correct syntax for?

The function argument to select the method is that The code is like this:

  array.sort (function (x, y) {var xp = x [3]; var yp = y [3] ; // The fourth column I used in this example ... back to xp == yp? 0: xp   

Edit

After your comment, here is a small demo function that should help in understanding that How does it work.

Instead of using small forms / I used the traditional form in the second position and explained it in 3 lines, which makes it easy to understand.

  function demo () {// Using the full sheet as the array source array = spreadsheet app.active (). GetActiveSheet () GetDataRange () GetValues ​​(); Logger.log ('Unsorted array =' + array); Array (function (x, y) {// In this example I used the 4th column ... var compareArgumentA = x [3]; var compared to arguments = y [3]; // eventually these 2 variables Let's do something with, the example number (x [0]) and the array for the number (y [0]) (index 0) // second example x [0] .toLowerCase () and y [0] in numerical column Values ​​will compare .toLowerCase () without the care of letterCase ... Logger.log ('comparisonAragment A =' + Comparison Agreement A + "and comparison B = '+ CompareArjimant B); var results = 0; // Start the return value and then compare: 3 cases if (comparisonArgament A == comparisonAbstract B) {return result}; // If equal return 0 if (comparison agreement A and LT; comparisonAbstract B) {Results = -1; Return Results}; // If A & L; B Return -1 (You can definitely change this and reverse sort order) if (compare before A & gt ; ComparativeAragment B) {Results = 1; Refund Result}; // if a & gt; B Return 1}); Logger.log ('\ n \ n \ n Simplified array =' array); }   

I added some logger.log to check for intermediate and final values. See it in a spreadsheet

This will help to hope.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -