ID:271378
 
Does anyone know how I would be able to sort a list into alphabetical order? Ive been stumped on this a bit.

Here the list.
var/list/L=list("Z","z","d","j","A","Z")


I want it to come out like this.
L=list("A","d","j","Z","Z","z")


Some help would be nice :).

- Darky
You will have to write your own sorting function. A simple one to learn is bubblesort, and is fine to use on small lists. If you are going to be sorting this list often or the list is going to grow fairly large, you will want to change sorting algorithms.

A bubble sort for numbers would look like this:
proc/bubblesort(list/L)
for(var/i = L.len, i > 0, i--) // start iterating backwards
for(var/j = 1, j < i, j++) // for each iteration backwards, iterate forwards until the 'i' counter is hit
if(L[j] > L[j+1]) // is the first number larger than the next number?
L.Swap(j, j+1) // if so, bring it down the list


For more information of how these sorts work, there are a massive amount of resources on these topics online (since sorts refer to all programming languages). There are also some pretty useful libraries on BYOND that can do this for you.

DM has a nifty sorttext() function. It doesn't do any actual sorting, but it can tell you if a letter is before or after alphabetically, since in your case you want a case insensitive sort in letters. You can use this proc inside of your sorting algorithm. Look up the proc, and you should be able to modify the sort to sort for text.
In response to Unknown Person
I believe > and < (and all their variants) work on strings, too.
In response to Jp
Jp wrote:
I believe > and < (and all their variants) work on strings, too.

Yep they do, but they are equivalent to sortText(), which wouldn't do what Dark Emrald wants, since his is case insensitive.
In response to Unknown Person
You may want to lowertext when comparing since < and > compare in ascii order, not alphabetical.
In response to Xx Dark Wizard xX
Xx Dark Wizard xX wrote:
You may want to lowertext when comparing since < and > compare in ascii order, not alphabetical.

Unknown Person wrote:
DM has a nifty sorttext() function. It doesn't do any actual sorting, but it can tell you if a letter is before or after alphabetically, since in your case you want a case insensitive sort in letters. You can use this proc inside of your sorting algorithm. Look up the proc, and you should be able to modify the sort to sort for text.
In response to Unknown Person
Thanks for all your help (Y). Got it working.

- DE