可以直接利用Sql查询来排序,效率不一定高,但是代码简单,避开了排序算法本身.
程序代码:
create cursor b1 (m1 c(3),m2 c(3) ,m3 c(3))
insert into b1 values ('132','123','')
insert into b1 values ('301','013','')
insert into b1 values ('132','123','')
insert into b1 values ('211','112','')
insert into b1 values ('121','112','')
insert into b1 values ('200','002','')
UPDATE b1 SET b1.m2 = func_px(b1.m1)
UPDATE b1 SET b1.m3 = func_px(b1.m1,.t.) && 倒序排列
FUNCTION func_px(str1 as String ,SortDesc as Boolean)
* 参数1,欲排序的字符串,参数2,输出倒序还是正常顺序? 默认正常排序.
IF USED('_px') THEN
USE IN _px
ENDIF
CREATE CURSOR _px (s1 C(1))
LOCAL ii as Integer ,str2 as String
str2=""
FOR ii = 1 TO LEN(str1)
INSERT INTO _px VALUES (SUBSTR(str1,ii,1))
ENDFOR
IF SortDesc
SELECT s1 FROM _px INTO CURSOR _px2 ORDER BY s1 DESC
ELSE
SELECT s1 FROM _px INTO CURSOR _px2 ORDER BY s1
ENDIF
USE IN _px
SELECT _px2
GO TOP
SCAN
str2 = str2 + _px2.s1
ENDSCAN
USE IN _px2
RETURN str2
ENDFUNC
也可以利用数组,先装入数组,再用Asort()排序.这个就不举例了.
但是Asort()只能对字符型排序,而上面这个算法,数值型,日期型都可以排序.
[此贴子已经被作者于2021-8-4 17:26编辑过]