Cancel
Showing results for
Did you mean:

# Sorting a nested array

Creator

Hi,

I am trying to sort a nested array in a method, based on the first element of every array in the nested array. I tried using sort, as it sorts the first column of n-dimensional arrays but it doesn't work. I guess it's not meant to work for nested arrays, only for n-dimensional arrays.

I want to use nested arrays because I need to be able to use the append function (I'm appending arrays to an array), I don't know the dimensions of my nested array at the start of the method should I want to just use n-dimensional arrays instead.

Is there a way to sort these using standard methods? Or should I program my own sorting method (I don't know which algorithm is used in the standard sort)?

Example of what I want to do:
[[13, object1], [12, object2], [5, object3], [20, object4]]

becomes:

[[5, object3], [12, object2], [13, object1], [20, object4]]
Example conclusion:
I now know that object 3 has the shortest calculated distance

Example model in attachment:

I'm trying to use a nested array to print a sorted list of destinations (tracks) and their distances in a table according to shortest distance.

Thanks,

Seppe

2 REPLIES

# Betreff: Sorting a nested array

Gears Phenom

Hi Seppe,

from the use-case you show here I see no need at all to use arrays (and definitely no nested arrays). The table offers all functionalities you need:

```var objDest	: object

-- clear table
SortedDistances.delete({0,1}..{*,*})

-- fill table with destinations and distances
for var i := 1 to 8
-- get destination track
objDest := str_to_obj(to_str("Dest",i))

-- insert into table by sorted distance
SortedDistances.inOrder(
"Distance", 	Start.getRouteLength(objDest), 	"up",
"Destination",	objDest,						"")
next```

Does this help?

Alex

___________________________________________________________
Alex Dilg, Consultant at SimPlan AG (www.SimPlan.de)
This post was good and/or helpful to you? Please provide some Kudos, thank you!

# Betreff: Sorting a nested array

Creator

Hi Alex,

Thank you! This is indeed a more elegant (and possible) way! I didn't know about the .inOrder() method.

Greetings,

Seppe