| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

[分享]二维数组按指定列降序排列，应该有更简单的方法。

clear
DIMENSION againiancombo[4,2]
againiancombo[1,1] = "one"
againiancombo[2,1] = "two"
againiancombo[3,1] = "three"
againiancombo[4,1] = "four"
againiancombo[1,2] = 1
againiancombo[2,2] = 2
againiancombo[3,2] = 3
againiancombo[4,2] = 4
prn(@againiancombo)
CREATE CURSOR tt (f1 C(10), f2 I)
APPEND FROM ARRAY againiancombo
INDEX on f1 TAG f1 desc
COPY TO ARRAY arr
prn(@arr)
INDEX on f2 TAG f2 desc
COPY TO ARRAY arr
prn(@arr)
RETURN

FUNCTION prn(arr)
?
FOR i=1 TO 4
?
FOR j=1 TO 2
?? arr[i,j]
ENDFOR
ENDFOR
ENDPROC

```
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编辑过]

[此贴子已经被作者于2017-12-19 17:32编辑过]

• 5
• 1/1页
• 1