ID:169699
 
Consider this list:

var/list/L = list("Cake","Chocolate","Pie")


I'm turning everything in the list to a number. Then I'm bubble sorting that number list(for those who don't know what bubble sorting is, it organizes a list of numbers in ascending or descending order).

So lets say the new number list looks like this:

var/list/Lnum = list(1,2,3)


How can I now turn Lnum back into L but still sorted?

I know that sounds confusing so I'll explain it a bit:

Lets assume:

Cake = 3
Pie = 2
Chocolate = 1

After bubble sorting it, I get the list "1,2,3".

How can I now turn the new list into "Chocolate,Pie,Cake" as opposed to the original "Cake,Chocolate,Pie"?
DeathAwaitsU wrote:
How can I now turn the new list into "Chocolate,Pie,Cake" as opposed to the original "Cake,Chocolate,Pie"?

Well, for one, it makes a whole lot more sense just to sort the list in the first place instead of getting a list of numbers, sorting that, and then converting the number list back into its original medium.

How exactly to accomplish this, of course, depends alot on what parameter you're sorting by.

If you're sorting a list of text, for example, you'll probably want an associative list in the format, element_to_be_sorted == parameter_to_sort_by. In that case, you could use a proc like this:

//Sorts L from greatest to least by each key's value
proc/associative_bubble_sort(list/L)
for(var/x = 1,x <= L.len,x++)
for(var/remain = L.len,remain > 1,remain--)
var/comparison_a = L[remain-1]
var/comparison_b = L[remain]
//Make sure that numbers are being compared
ASSERT(isnum(L[comparison_a]) && isnum(L[comparison_b]))
if(L[comparison_a] < L[comparison_b]) L.Swap(remain - 1,remain)


If you want to test it, try the following verb out:

mob/verb/associative_bubble_sort_test()
var/list/test = list("a"=32345,"b"=2345,"d"=245,"f"=25,\
"e"=65,"g"=3,"i"=1,"c"=564,"h"=3)
associative_bubble_sort(test)
for(var/elem in test)
src << "test\[[test.Find(elem)]\] = [elem]"

/*Outputs:

test[1] = a
test[2] = b
test[3] = c
test[4] = d
test[5] = e
test[6] = f
test[7] = g
test[8] = h
test[9] = i
*/


Please note that associative_bubble_sort() MUST take a completely associative list as an argument.

If you want to sort a non-associative list, then use this proc, which I programmed for you in another post:

//Sorts L from greatest to least.
proc/bubble_sort(list/L)
for(var/x = 1,x <= L.len,x++)
for(var/remain = L.len,remain > 1,remain--)
if(L[remain-1] < L[remain]) L.Swap(remain - 1,remain)
In response to Wizkidd0123
Wizkidd0123 wrote:
If you want to sort a non-associative list, then use this proc, which I programmed for you in another post:

> //Sorts L from greatest to least.
> proc/bubble_sort(list/L)
> for(var/x = 1,x <= L.len,x++)
> for(var/remain = L.len,remain > 1,remain--)
> if(L[remain-1] < L[remain]) L.Swap(remain - 1,remain)
>


Surely that would only work if the list had numbers in it wouldn't it? You cant do if("Pie"<"Chocolate").
In response to DeathAwaitsU
Surely that would only work if the list had numbers in it wouldn't it? You cant do if("Pie"<"Chocolate").

We need operator overloading :P.
In response to DeathAwaitsU
DeathAwaitsU wrote:
//Sorts L from greatest to least.
proc/bubble_sort(list/L)
for(var/x = 1,x <= L.len,x++)
for(var/remain = L.len,remain > 1,remain--)
if(L[remain-1] < L[remain]) L.Swap(remain - 1,remain)

Surely that would only work if the list had numbers in it wouldn't it? You cant do if("Pie"<"Chocolate").

Yup. If you want to check if("Pie" < "Chocolate"), and you have numerical values with which you can use to sort your text values, then you should use associative_bubble_sort() on an assoc. list in the format, text_value == numerical_value_used_for_sorting.