Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   this.array.sortascending() Help? (https://forums.graalonline.com/forums/showthread.php?t=134261594)

kingcj 01-07-2011 08:49 PM

this.array.sortascending() Help?
 
Hello,
I realize that sortascending() sorts from least to greatest, but I am having a problem when the greatest is over single digits. I'm guessing that it only reads the first digit to place the item into the array, and I don't see another way around this. Any suggestions would be appreciated!

xXziroXx 01-07-2011 11:16 PM

Quote:

Originally Posted by Stefan (Post 1419897)
Quoting http://forums.graalonline.com/forums...81&postcount=2 :

For the tree view you set attributes like this:

sortmode = "value";
sortorder = "descending";
groupsortorder = "ascending";

For the nodes then:

sortvalue = 1;
sortgroup = 2;

That would sort the nodes after the value you have assigned to them. When you set sortmode="name" then it will be sorted alphabetically. The sort group can be used to let some types of nodes always appear at the top, e.g. in a folder view you would want to display the folders at the top.

---

sortmode can be "value", "name" or "extension"

You basically assign a value to each entry adn sort by value.

WhiteDragon 01-07-2011 11:32 PM

Quote:

Originally Posted by xXziroXx (Post 1621127)
You basically assign a value to each entry adn sort by value.

Uh, this works for sortascending? I thought it was for GUIs.


The problem is that it is sorting by the string value, rather than the number value.
i.e.,
{"aaa", "aab", "ba", "c"} is sorted by the string value.
{111, 112, 21, 3} is sorted by the string value, when it should really be sorted by float value into: {3,21,111,112}.


To my knowledge there are two ways to solve this:

Way 1
Store the number in a subvariable for each item in the array, then use sortbyvalue:
PHP Code:

temp.xs = {1,6,7,3,2,1,5,23,41,75,2347,51,123};

for (
temp.0temp.temp.xs.size(); temp.i++) {
  
temp.xs[temp.i].blah temp.xs[temp.i];
}
temp.xs.sortbyvalue("blah""float"true); 

Way 2
Use quicksort, which lets you specify your own function for comparing two values. (See examples in the link.)

kingcj 01-07-2011 11:59 PM

Quote:

Originally Posted by WhiteDragon (Post 1621132)
Uh, this works for sortascending?
Way 1
Store the number in a subvariable for each item in the array, then use sortbyvalue:
PHP Code:

temp.xs = {1,6,7,3,2,1,5,23,41,75,2347,51,123};

for (
temp.0temp.temp.xs.size(); temp.i++) {
  
temp.xs[temp.i].blah temp.xs[temp.i];
}
temp.xs.sortbyvalue("blah""float"true); 


This worked for me, but I'm unsure why the .blah was needed when assigning. I'm not sure if the other is for GUIs or not, but thanks for the help!

xXziroXx 01-08-2011 12:07 AM

Quote:

Originally Posted by WhiteDragon (Post 1621132)
Uh, this works for sortascending? I thought it was for GUIs.

Oops, seems I misunderstood the OP.

fowlplay4 01-08-2011 12:08 AM

Quote:

Originally Posted by kingcj (Post 1621137)
This worked for me, but I'm unsure why the .blah was needed when assigning. I'm not sure if the other is for GUIs or not, but thanks for the help!

That's the variable sortbyvalue grabs the value from to sort it by, you could use rabblerabble if you wanted to.

I.e:

PHP Code:

temp.xs = {1,6,7,3,2,1,5,23,41,75,2347,51,123}; 
for (
temp.0temp.temp.xs.size(); temp.i++) { 
  
temp.xs[temp.i].rabblerabble temp.xs[temp.i]; 

temp.xs.sortbyvalue("rabblerabble""float"true); 


kingcj 01-08-2011 12:37 AM

Quote:

Originally Posted by fowlplay4 (Post 1621145)
That's the variable sortbyvalue grabs the value from to sort it by, you could use rabblerabble if you wanted to.

I.e:

PHP Code:

temp.xs = {1,6,7,3,2,1,5,23,41,75,2347,51,123}; 
for (
temp.0temp.temp.xs.size(); temp.i++) { 
  
temp.xs[temp.i].rabblerabble temp.xs[temp.i]; 

temp.xs.sortbyvalue("rabblerabble""float"true); 


Oh... Thank you very much!


All times are GMT +2. The time now is 08:27 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.