[分享]二维数组按指定列降序排列,应该有更简单的方法。
新建 压缩(zipped)文件夹.zip
(1.06 KB)
PROCEDURE SortByCursor PARAMETERS aname,lie,SortType * 对指定二维数组按指定列、指定排序方式排列,用法[color=#808080]=SortByCursor(cArrayName,[n列序号],["A"|"D"])[/color] * 参数说明:参数1:数组名; * 参数2:列序号,整数型(序号从1开始),参数2可省略,如省略,默认值为1 * 参数3:升序A([color=#0000FF]ASC ,缩写为A)或降序D(DESC ,缩写为D)[/color] * 参数3可省略,如省略,默认值为A(升序) ,如不省略则只能有A、D两个选项,大小写均可。 * 将来可能的扩展:参数2可以是一个数组,或者由逗号分割的一个字串(比如“[color=#800000]2,3”)[/color] * 则表示需要按2、3列进行排序,这个功能暂时先不写,目前只保证按一个列排序。 * 厨师王德榜 [color=#800000]2017-12-19[/color] LOCAL nColCount as Integer ,ic as Integer,ir as Long LOCAL cSql as String ,cColType as String ,cOrder as String nColCount = ALEN(&aname,2) * 参数检测: IF EMPTY(lie) lie =1 ENDIF lie = IIF(lie > nColCount ,nColCount ,lie) IF UPPER(SortType)="D" cOrder=" Desc" ELSE cOrder = " " ENDIF * 根据数组首行值的特征,建立对应游标(不足之处:只能根据首行特征建立列) IF USED('TmpCC') USE IN TmpCC ENDIF cSql = "Create cursor TmpCC (" FOR ic =1 TO nColCount cColType = TYPE([&aname(1,ic)]) DO CASE CASE cColType ="C" cColType =" c(250)" CASE cColType ="N" cColType =" N(19,4)" CASE cColType ="I" cColType =" I" CASE cColType ="Y" cColType =" Y" CASE cColType ="D" cColType =" D" CASE cColType ="T" cColType =" T" CASE cColType ="L" cColType =" L" OTHERWISE cColType =" c(250)" ENDCASE cSql = cSql + 'Col' + LTRIM(STR(ic)) + cColType + IIF(ic=nColCount ,")" ,",") ENDFOR &csql * 把数组装入游标: FOR ir =1 TO ALEN(&aname,1) csql="INSERT INTO TmpCC values(" FOR ic =1 TO nColCount csql= csql + aname + "[" + LTRIM(STR(ir)) + "," + LTRIM(STR(ic)) + "]" ; + IIF(ic=nColCount ,")" ,",") ENDFOR &csql ENDFOR * 根据参数2,用SQL语句排序。结果输入到数组。 csql="Select * from TmpCC order by Col" + LTRIM(STR(lie)) + cOrder ; + " into Array " + aname &csql USE IN TmpCC ENDPROC
[此贴子已经被作者于2017-12-19 13:38编辑过]