Clarification on Sort

How do I utilize it effectively? It sorts only the keys but not change the values which seems to make it not very useful.

Here is an example of how I am trying to use it.

Code:
int[item] test;
for x from 1 to 5
	test[to_item(x)]=x;
print("Map Reference:", "orange");
foreach it in test
	print(test[it]+": "+it, "blue"); 
print("");
sort test by -value;
foreach it in test
	print(test[it]+": "+it, "blue");

It gives
Code:
Map Reference:
3: helmet turtle
5: pasta spoon
2: seal tooth
1: seal-clubbing club
4: turtle totem

5: helmet turtle
4: pasta spoon
3: seal tooth
2: seal-clubbing club
1: turtle totem

It sorts the keys how I want them but it doesn't match them with the correct output. So, I decided to make a copy of the map, sort that, then put its sorted keys into the original map which would give me the outputs in a sorted fashion.

Code:
int[item] test;
int[item] test2;
for x from 1 to 5
	test[to_item(x)]=x;
print("Map Reference:", "orange");
foreach it in test
	print(test[it]+": "+it, "blue"); 
print("");
test2 = test;
sort test2 by -value;
print("Map Test1:", "orange");
foreach it in test
	print(test[it]+": "+it, "blue"); 
print("");
print("Map Test2--Sorted:", "orange");
foreach it in test
	print(test2[it]+": "+it, "blue");

It gave
Code:
Map Reference:
3: helmet turtle
5: pasta spoon
2: seal tooth
1: seal-clubbing club
4: turtle totem

Map Test1:
5: helmet turtle
4: pasta spoon
3: seal tooth
2: seal-clubbing club
1: turtle totem

Map Test2--Sorted:
5: helmet turtle
4: pasta spoon
3: seal tooth
2: seal-clubbing club
1: turtle totem

Which is not what I wanted, it sorted both maps even tho I said to only sort test2. Not sure why it did that and not sure how to use the command effectively. :(
 

Bale

Minion
test2 = test1 is an assignment by reference, not by value. In other words, they are the same map. To do what you were intending it needs to be done like this;

Code:
foreach key in test1;
   test2[key] = test1[key];

I know that seems unnecessarily complicated, but that's how it is. Now on to the issue of how to use sort. This is only useful when you are sorting an ordered list. Here's an example:

Code:
item [int] stuff;
foreach it in get_inventory()
   if(historical_price(it) > 0)
      stuff[ count(stuff) ] = it;

sort stuff by historical_price(value);

foreach key, it in stuff
   print("Price of "+it+": "+historical_price(it));

That will sort your entire inventory by sale price. Note that the key is simply an ordered set of integers. That is important because we are simply altering the order of assigning values to keys. The purpose of the key is just to provide an ordering.
 
Thanks for your clarifications, they make sense. Finally got it to do what I wanted :D

Code:
item[int] test;

for x from 1 to 5 
	test[x]=to_item(x);

print("Map Reference:", "orange");
foreach it in test
	print(test[it]+": "+to_int(test[it]), "blue"); 
print("");
sort test by -to_int(value);
print("Map Test1:", "orange");
foreach it in test
	print(test[it]+": "+to_int(test[it]), "blue");

It gives:
Code:
Map Reference:
seal-clubbing club: 1
seal tooth: 2
helmet turtle: 3
turtle totem: 4
pasta spoon: 5

Map Test1:
pasta spoon: 5
turtle totem: 4
helmet turtle: 3
seal tooth: 2
seal-clubbing club: 1

Thanks so much Bale!
 
Top