This FlexSim Community Forum is read-only. Please post any new questions, ideas, or discussions to our new community (we call it Answers) at Our new Question & Answer site brings a modern, mobile-friendly interface and more focus on getting answers quickly. There are a few differences between how our new Q&A community works vs. a classic, threaded-conversation-style forum like the one below, so be sure to read our Answers Best Practices.

Go Back   FlexSim Community Forum > FlexSim Software > Tips and Tricks

Tips and Tricks Share helpful modeling ideas

Old 07-28-2009
Ben Wilson's Avatar
Ben Wilson Ben Wilson is offline
Flexsim Consultant, Super Moderator
Join Date: Jul 2007
Posts: 82
Downloads: 352
Uploads: 6
Thanks: 30
Thanked 159 Times in 43 Posts
Rep Power: 256
Ben Wilson is a glorious beacon of lightBen Wilson is a glorious beacon of lightBen Wilson is a glorious beacon of lightBen Wilson is a glorious beacon of lightBen Wilson is a glorious beacon of light
Default Sorting a list of nodes in the tree

If you have ever needed to sort a list of number nodes in your tree, read on...

Perhaps you resorted to using 2 nested for loops. This is probably fine for quite small lists, but as you get up to dozens or hundreds of nodes in your list, that method is quite inefficient. Especially if you're sorting the list during the simulation run.

I've coded up two of the best list sorting algorithms to work in Flexsim's tree. This will be somewhat of an advanced exercise unless you have a good computer science background, or you're fairly experienced in Flexsim's tree structure and flexscript. If this doesn't describe you, but you still need to sort a list, feel free to post any questions below.

If you download this file and extract it, you'll find two .t files. Each of the .t files is a user command and should be added to your model in this location:


If you don't have those nodes in your tree, first try opening the User Command interface (Main menu > Tools > User Commands) and adding a new user command. Then close the User Commands window and dive into the tree again.

After adding these .t files to nodes in your tree, you may need to reopen the User Commands GUI, and perhaps close it again, in order to register the new commands. Now you should be able to use these new commands.

Quicksort and mergesort are among the fastest algorithms for sorting a list. See and to learn more.

Both of these user commands take the same set of parameters and will end up with the same result - a sorted list of numeric nodes. However, the guts of each command are quite different. One command may be much more efficient (execute faster) for your list of data than the other. If you're just doing a one-time sort, then it probably doesn't matter which command you use. However, if for some reason you're running the command while your simulation is running, you'll want to experiment with both commands to see which gives you better performance. It will largely depend on the size and randomness of your list of nodes.

With just a few modifications these commands could be used to sort a table, in place of Flexsim's sorttable() command. This might be your solution if you've wanted to use sorttable() to sort in descending order, which these user commands can handle.

As a quick reference, you can use these commands as follows:

treenode mylist = rank(mydata, i);

This will use default values to sort mylist in ascending order. Or you can enter in some optional parameters to change the sorting order or sort just a portion of the list. Check out the user command descriptions from the User Command interface for more info.

Hopefully these will be helpful to someone.

Attached Files
File Type: zip (5.5 KB, 331 views)

Last edited by Ben Wilson; 07-29-2009 at 08:22 AM.
The Following 11 Users Say Thank You to Ben Wilson For This Useful Post:
zhouzheng (07-29-2009)
Old 02-26-2013
AlanZhang's Avatar
AlanZhang AlanZhang is offline
Flexsim Super Moderator
Join Date: Aug 2007
Location: CA
Posts: 289
Downloads: 64
Uploads: 0
Thanks: 88
Thanked 91 Times in 47 Posts
Rep Power: 225
AlanZhang is a jewel in the roughAlanZhang is a jewel in the roughAlanZhang is a jewel in the rough

It seems the above quicksort does not work in some cases. mergsort is fine.

Use quicksort(so(), 1, 1, 10) on the following list:

It will return:
Old 02-26-2013
Carsten Seehafer's Avatar
Carsten Seehafer Carsten Seehafer is offline
FlexSim Geek
Join Date: Oct 2008
Location: Ritterhude, Deutschland
Posts: 230
Downloads: 45
Uploads: 1
Thanks: 474
Thanked 320 Times in 143 Posts
Rep Power: 379
Carsten Seehafer has much to be proud ofCarsten Seehafer has much to be proud ofCarsten Seehafer has much to be proud ofCarsten Seehafer has much to be proud ofCarsten Seehafer has much to be proud ofCarsten Seehafer has much to be proud ofCarsten Seehafer has much to be proud ofCarsten Seehafer has much to be proud ofCarsten Seehafer has much to be proud of

It's a small error in the loops of quicksort:

Add the "=" in row 29 and 49:
for(int i=minrank; i<=maxrank; i++){
The Following User Says Thank You to Carsten Seehafer For This Useful Post:
Ben Wilson (11-20-2013)
Old 02-27-2013
Vincent Bechard Vincent Bechard is offline
Consulting in Simulation
Join Date: Oct 2008
Location: Montréal (Canada)
Posts: 28
Downloads: 0
Uploads: 0
Thanks: 4
Thanked 41 Times in 13 Posts
Rep Power: 165
Vincent Bechard will become famous soon enoughVincent Bechard will become famous soon enough
Lightbulb Try O-Sort!

Here is an interesting technique I have used for the past years:

For small vectors (less than 25 entries), it is faster than bubble sort. For large vectors (more than 10,000 entries), I cannot differentiate between quick sort and o-sort in terms of speed. What was reported by the author is kind of a worst case scenario.

From my opinion, this algorithm is very attractive because of its simplicity, stability, speed, exactness and by the fact it is efficient for small and large vectors.
Vincent Béchard, Eng., MASc.
Discrete Event Simulation Designer
SNC-Lavalin, Industrial Division

Thread Thread Starter Forum Replies Last Post
Sorting and Stacking multi products Simon Zaw Q&A 5 08-05-2014 01:16 AM
The nodes in active view tree don't have unique names. Tom David Gripes and Goodies 7 01-31-2013 07:01 AM
Queue Sorting Jeff Nordgren Q&A 12 05-22-2008 03:21 PM
Accessing Data of a Recorder and Referencing Nodes in the Tree Jan Brandau Q&A 5 04-22-2008 07:05 AM
Access to the tree variables Iulian Marin Ion Q&A 2 02-06-2008 09:15 AM

All times are GMT -6.
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Copyright 1993-2018 FlexSim Software Products, Inc.