ID:1299791
 
I just figured I'd post this because since the last time I was actually working on a project in DM (Which was a while ago) I remember in every failed turn based RPG I created that I needed sorting (speed determining order of turns) then I remembered that I always used to use a sorting library with tons of code in it for this.

Anyways, as I was reading through the DM guide I realized you can past lists into min() and max() (I never knew this, I always thought it only took two arguments)

So yeah, here's a really simple sorting snippet:
var/list/List=list()
var/list/orderedList=list()
for(var/i=1 to 10)
List+=rand(1,100)
for(var/i=1 to List.len)
var/item = max(List)
orderedList+=item
List-=item
world<<item


I'm sure this probably isn't news to most, but I know turn-based RPG's tend to be a favorite amongst the developers I've talked to, and perhaps this'll help out any newbies.

I sure wish I would've known about it considering the countless number of times I struggled with simple numerical sorting.

Cheers
This will sort but it's a fairly intensive sorting algorithm. Has n-squared complexity. In an environment like Byond you'd really want to be using a less intensive algorithm.

If you're not familiar with sorting algorithms here's a page that lists them with their complexity:
https://en.wikipedia.org/wiki/Sorting_algorithm

I'd probably advise merge sort as being better:
https://en.wikipedia.org/wiki/Merge_sort

It's reasonably easy to do in BYOND and the code is quite translatable.
I think given what it's used for it wouldn't be very intensive at all (I intended it for turn-based combat)

It's not like it'll be getting called thousands of times per tick, haha.
In response to Zecronious
It's probably fair to say folks won't be using any log(n) or n log(n) sorting around here though, either, so it could be far worse than n^2.
In response to DivineTraveller
Please explain how using Merge Sort would be worse?
In response to Ill Im
Ill Im, you're posting the code for everyone to use. It's not just used in your code anymore, you put it out for everyone. That's why it's in Tutorials/Snippits.

So with that being said, there are better alternatives for sorting than what you posted, hence I pointed one out.