一种列排序程序
原文链接:https://www.nv5geospatialsoftware.com/Learn/Blogs/Blog-Details/a-column-sort-routine
19012 评价本文:
尚未评分
一种列排序程序
匿名 星期四,2013年4月18日
在像 Excel 或 LibreOffice 这样的电子表格程序中,您可以将某一列的排序应用于电子表格中的每一列。IDL的 SORT 函数不提供此功能,但通过少量代码,我们可以实现它。函数 COLSORT(获取源代码请点击此处)接受一个二维数组和要作为排序依据的列(索引从零开始)。默认情况下,值按升序排序;可以设置关键字以按降序排序。以下是该程序如何工作的示例。首先是一个 4 x 5 的数字数组:
IDL> a = round(randomu(seed, 4, 5) * 20.0)
IDL> print, 'Original array:', a, format='(a,/,4(i))'
Original array:
8 6 14 10
4 9 18 5
1 11 13 18
8 9 11 9
3 19 4 16
使用 COLSORT 对列索引 1(第二列)执行反向排序,并将排序扩展到数组中的其他列:
IDL> sort_index = 1
IDL> b = colsort(a, sort_index, /reverse_sort)
检查结果:
IDL> print, 'Sorted (descending) array:', b, format='(a,/,4(i))'
Sorted (descending) array:
3 19 4 16
1 11 13 18
8 9 11 9
4 9 18 5
8 6 14 10
此程序可以扩展以应用于行和更高维度的数组。