谷歌脚本按任何列对二维数组进行排序 [英] Google Script sort 2D Array by any column
问题描述
我之前问过一个关于从数据库中检索记录的问题,这里:使用 Google Script 从 Google Sheet 中检索记录
I had a asked an earlier question about retrieving records from a database, here: Retrieving Records from a Google Sheet with Google Script
我对操作数组和创建自己的排序算法相当满意,但由于其速度,我想使用现有的 Array.sort()
方法来组织数据.我发现我可以很容易地使用它来按第一列数据对二维数组进行排序,但我找不到对不同数据列进行排序的语法,而不是第一列.
I'm fairly comfortable with manipulating arrays and creating my own sorting algorithms, but I want to use the existing Array.sort()
method to organize the data because of its speed. I'm finding that I can easily use this to sort a 2D array by the first column of data, but I can't find the syntax to sort on a different column of data, other than the first.
我发现的最接近的是:Google Apps Script Additional Sorting Rules.但是,这些输入对我不起作用.这是我从以下代码中得到的,对于我的数组 tableData:
The closest that I've found is this: Google Apps Script Additional Sorting Rules. However, these inputs haven't worked for me. Here is what I get for the following code, for my array, tableData:
tableData.sort([{ column: 1}]);
=>TypeError: (class)@4dde8e64 不是函数,它是对象.(第 49 行,文件sortTablebyCol")
=>TypeError: (class)@4dde8e64 is not a function, it is object. (line 49, file "sortTablebyCol")
tableData.sort([{column: 1, ascending: true}]);
=> TypeError: (class)@4d89c26e 不是函数,它是对象.(第 50 行,文件sortTablebyCol")
=> TypeError: (class)@4d89c26e is not a function, it is object. (line 50, file "sortTablebyCol")
选择要排序的数据列的正确语法是什么?
What is the proper syntax for choosing which column of data to sort on?
推荐答案
array.sort 方法可以有一个函数参数来选择要排序的部分.代码是这样的:
The array.sort method can have a function argument to choose on what part you want to sort. Code goes like this :
array.sort(function(x,y){
var xp = x[3];
var yp = y[3];
// in this example I used the 4th column...
return xp == yp ? 0 : xp < yp ? -1 : 1;
});
编辑
根据您的评论,这里有一个小的演示功能,应该有助于理解它是如何工作的.
EDIT
Following your comment, here is a small demo function that should help to understand how this works.
我没有使用简写形式的 if/else 条件,而是使用传统形式并将其分成 3 行以使其更易于理解.
Instead of using short form if/else condition I used the traditional form and splitted it in 3 lines to make it easier to understand.
function demo(){
// using a full sheet as array source
var array = SpreadsheetApp.getActive().getActiveSheet().getDataRange().getValues();
Logger.log('Unsorted array = '+array);
array.sort(function(x,y){
// in this example I used the 4th column...
var compareArgumentA = x[3];
var compareArgumentB = y[3];
// eventually do something with these 2 variables, for example Number(x[0]) and Number(y[0]) would do the comparison on numeric values of first column in the array (index0)
// another example x[0].toLowerCase() and y[0].toLowerCase() would do the comparison without taking care of letterCase...
Logger.log('compareArgumentA = '+compareArgumentA+' and compareArgumentB = '+compareArgumentB);
var result = 0;// initialize return value and then do the comparison : 3 cases
if(compareArgumentA == compareArgumentB ){return result }; // if equal return 0
if(compareArgumentA < compareArgumentB ){result = -1 ; return result }; // if A<B return -1 (you can change this of course and invert the sort order)
if(compareArgumentA > compareArgumentB ){result = 1 ; return result }; // if a>B return 1
}
);
Logger.log('
Sorted array = '+array);
}
我添加了几个 Logger.log 来检查起始值、中间值和最终值.在电子表格中试试这个.
I added a couple of Logger.log to check starting, intermediate and final values. Try this in a spreadsheet.
希望这会有所帮助.
这篇关于谷歌脚本按任何列对二维数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!