Skill语言实现将一个table中的坐标point(x,y)按照x和y进行从小到大排序的函数

本文介绍了一种用于对表格中的点按X或Y坐标进行排序的算法。该算法首先复制原始表格,然后使用冒泡排序法根据指定的坐标类型对点进行排序。提供了两个示例,分别展示了按X坐标和Y坐标排序的结果。
/*****************
对table中的point进行排序,按照type值将x或者y从小到大排
*******************/
defun(TableSort (table type)
	table_len=length(table)
	let((sortedTable)
		sortedTable=makeTable("table")
		for(i 0 table_len-1
			sortedTable[i]=table[i]
		)
		for(i 0 table_len-2
			for(j i+1 table_len-1
				if(type=="x" && car(sortedTable[i])>car(sortedTable[j]) then
					temp=sortedTable[i] 
					sortedTable[i]=sortedTable[j] 
					sortedTable[j]=temp
					
					else if(type=="y" && nth(1 sortedTable[i])>nth(1 sortedTable[j]) then
						temp=sortedTable[i] 
						sortedTable[i]=sortedTable[j] 
						sortedTable[j]=temp
					)
				)
					
			)
				
		)
		sortedTable=sortedTable
	)
)
mytable=makeTable("table")
mytable[0]=3:1
mytable[1]=1:2
mytable[2]=2:3
table1=TableSort(mytable "x")
table1_len=length(table1)
for(i 0 table1_len-1 axlUIWPrint(nil "table1[%d]=%P" i table1[i]))
/*
执行结果:
table1[0]=1:2
table1[1]=2:3
table1[2]=3:1
*/
mytable[0]=1:3
mytable[1]=2:2
mytable[2]=3:1
table1=TableSort(mytable "y")
table1_len=length(table1)
for(i 0 table1_len-1 axlUIWPrint(nil "table1[%d]=%P" i table1[i]))
/*
执行结果:
table1[0]=3:1
table1[1]=2:2
table1[2]=1:3
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冲冲冲~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值