ID:164276
 
Could anyone give me a example on how to sort associative lists? I would like to have a list like this put in order buy the highest value. Also how can I return the name of the first person in the list? I tried doing something like this Test[1], but I just get a index out of bounds runtime. Thanks for the help
var/list/Test=list("Guy1"=200,"Guy2" = 100,"Guy3" = 300)


check my members page, and on the left there is a list sorting demo or lib i forget
In response to Xx Dark Wizard xX
Xx Dark Wizard xX wrote:
check my members page, and on the left there is a list sorting demo or lib i forget

I tried passing my list through all three list procs and none would sort the list by there numerical value. What I am trying to is not put them in alphabetical order but put them in order like this example

Guy3
Guy1
Guy2

var/list/Test=list("Guy1"=200,"Guy2" = 100,"Guy3" = 300)


In response to Brokenleg
Hmmm. That lemme go look at it.
In response to Brokenleg
Use this to display the list,

proc/list2text(list[])
for(var/item in list)
. += "[item] = [list[item]]\n"
// Try that with selection sort
mob/verb/SortMe()
mylist = SelectionSort(mylist)
src << list2text(mylist)
In response to Xx Dark Wizard xX
Here is what I tried to do
proc/list2text(list[])
for(var/item in list)
. += "[item] = [list[item]]\n"

mob/verb/SortMe()
var/list/mylist=list("Guy1"=200,"Guy2" = 100,"Guy3" = 300)
mylist = SelectionSort(mylist)
src << list2text(mylist)


here is the results
Image Hosted by ImageShack.us
In response to Brokenleg
Do you want it backwards then, or to sort by comparing the associated value.
In response to Xx Dark Wizard xX
Xx Dark Wizard xX wrote:
Do you want it backwards then, or to sort by comparing the associated value.

yeah, by there associated value like this.
Guy3 = 300
Guy1 = 200
Guy2 = 100

Edit* basically this is just a list that will put players in order by how much hate they have generated towards this monster with this list. Then the person in the first spot will be its target.
In response to Brokenleg
I guess I could write a proc for ya, just tell me if it works :)
proc/Sort(list/L)
var/min,i,j
for(i = 1 to L.len-1)
min = L[L[i]]
for(j = i + 1 to L.len)
if(L[L[j]] < min)
min = j
L.Swap(i,min)
return L

In response to Xx Dark Wizard xX
Sweet, it works like a charm thanks alot. I didn't realize you could access the associative list values like this L[L[j]].
In response to Brokenleg
L[L[i]] actually gets L[i] which could be the text string and access it in L you can also get the value with L["player"], glad it works.