Notifications
Clear all

[Closed] Sort array of objects by xPosition to helper

i have a array of objects , i want to sort my array by checking distance of xposition of objects to a Dummy helper.

can anyone help me how do that ?

thankx

5 Replies
fn compareDistance obj1 obj2 = (distance obj1 objhelper)>(distance obj2 objhelper)   
qSort listObjs compareDistance
4 Replies
(@siahpoosh)
Joined: 10 months ago

Posts: 0

thank u dude , but i got this error : Unable to convert: false to type: Integer

my code :


objhelper = $Dummy001
fn compareDistance obj1 obj2 = (distance obj1 objhelper)>(distance obj2 objhelper)   
qsort preHelperArrayTop compareDistance
qsort preHelperArrayBottom compareDistance

(@batigolnguyen)
Joined: 10 months ago

Posts: 0
fn compareDistance obj1 obj2 = 
(
	if ((distance obj1 objhelper)>(distance obj2 objhelper) ) then 
		return 1
	else
		if ((distance obj1 objhelper)<(distance obj2 objhelper) ) then 
			return -1
		else
			return 0
	
)
(@siahpoosh)
Joined: 10 months ago

Posts: 0

thank u very much dude

(@denist)
Joined: 11 months ago

Posts: 0

don’t use return to break a condition in mxs function.
but much faster sort algorithm could be:

collect node’s id(index) in list, and its distance to the helper as point2 item

qsort by second component