ID:264859
 
Code:
Sort_grid()
set
hidden = 1
var/namesort = winget(src, "map.sort1","is-checked=true")
var/list/sortlist = list()
var/list/itemlist = list()
var/position = 1
var/position2 = 1
var/sortpos = 1
var/maxpos = 0
var/obj1
var/obj2
if(namesort == "true")
if(src.viewer == "Inventory")
for(var/obj/item/I in usr.contents)
sortlist += I.name
itemlist += I.name
maxpos = sortlist.len
var/list/sorted[sortlist.len]
startnamesort //for goto
for(position2 = 1; position2 < maxpos; position2 ++)
if(!sorted.Find(itemlist[position2]))
obj1 = itemlist[position2]
else
continue
for(position = 1; position < sortlist.len; position++)
obj2 = sortlist[position]
switch(sorttext("[obj1]","[obj2]"))
if(1)
sorted[sortpos] = obj1
if(-1)
sorted[sortpos] = obj2
obj1 = sorted[sortpos]
sortlist -= sorted[sortpos]
sortpos++
if(sorted[maxpos] == null)
world << "goto"
position2 = 1
sortpos -= 1
goto startnamesort


Problem description:
It's very likely that I'm making this way more complicated than it seems, but here goes. Basically, I'm implementing some sorting functions to the grids for my game that will let you sort the contents based on different parameters (IE name, item type, element, etc).

I've spent a couple hours sorting this out, and I've got it to the point where it'll sort everything in order EXCEPT for the last object in the list, whatever happens to be last in the alphabetical order list. The reason is because the sortpos var is set at one.

Simply solution right? Set it to 0. Nope, it make dream seeker crash doing that. I have no idea why. Any ideas?

Thanks guys.

It appears first element in sorted list is actually empty entry. I failed to find how to fix it.

Alternatively I could offer to use Bubble Sort:
        Bubble_Sort()
var/list/sortlist = list()
for(var/obj/I in usr.contents)
sortlist += I.name

var/swaps
do
swaps = 0
for(var/i = 1; i < sortlist.len; i++)
if(sorttext(sortlist[i], sortlist[i + 1]) == -1)
sortlist.Swap(i, i + 1)
swaps++
while(swaps)

world << "sortlist is sorted:"
for(var/A in sortlist)
world << A
In response to Ripiz
Ah yes that should do nicely. Thank you sir, I greatly appreciate it. Its been almost a year and a half since I've worked with a bubblesort that I completely forgot about it.
In response to Pyro_dragons
Also, check AbyssDragon's SortProcs library. Plently of useful sorting algorithms.