Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi
Tinderbox Users >> Agent, Actions, Rules & Automation >> Agent Sorting-- Numbers
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1470772454

Message started by Steve Scott on Aug 9th, 2016, 3:54pm

Title: Agent Sorting-- Numbers
Post by Steve Scott on Aug 9th, 2016, 3:54pm

I'm trying to sort by a user attribute that is a number.  

I'm getting

1
10
2
3

I'd rather get

1
2
3
4
5
6
7
8
9
10

Any thoughts  on how to get numerical order?

Title: Re: Agent Sorting-- Numbers
Post by Mark Anderson on Aug 9th, 2016, 4:02pm

How about a list number sort: .nsort()?

Title: Re: Agent Sorting-- Numbers
Post by Steve Scott on Aug 9th, 2016, 4:23pm

Thanks, Mark.  Sometimes I don't have the vocabulary to know what I'm looking for! I only saw one other reference to an nsort in the forums.  I read the atbref link, but I don't how to use or update myString.  Do I add "$MyList = $MyList.nsort($Name)" into the my  Can you give me a bump in the right direction?  

Here is one (of many) things that I have tried.

The attribute that I want to sort is called "Unit"

1. Added MyString attribute to agent and pasted, "$Unit = $Unit.nsort($Name)"
2. In the agent I am trying to sort by MyString

Am I even close?

Title: Re: Agent Sorting-- Numbers
Post by Mark Anderson on Aug 9th, 2016, 5:20pm

List (and Set) type attributes are stored as strings under-the-hood, i.e. a single (text) string of all the values in the list with each value delimited by a semi-colon. Incidentally don't try using list values containing a semi-colon - you can probably guess why.

So into a List, $MyList, we add in this order four values: 1, 4, 13 and 2. Seen as a key attribute, the list is shown in it's stored concatenated string state, i.e "1;4;13;2". Note, although we're are storing only numbers the values are still stored as a string. There is no such thing as a Number List type.

Now to sort it. In each case below, we'll use a rule to sort $MyList differently, putting the result in $Text with each result on a different line (using .format)

$Text = $MyList.format('\n")

gives the underlying stored (unordered) list value:

1
4
13
2


Note that the 'unsorted' state of a Set [sic] type isn't necessarily predictable, so sorting is often advisable if making a string out of a Set. Now we'll use the 'normal .sort:

$Text = $MyList.sort.format('\n")

gives the list value sorted as if they are strings:

1
13
2
4


Note, that if passing no parameters we can omit the parentheses after .sort - this would work equally well though:

$Text = $MyList.sort().format('\n")

Although there is an .sort() for case-insensitive text sorting there is not point using it here - it sort numbers the same as .sort().
To get a number sort:

$Text = $MyList.nsort.format('\n")

.nsort assumes all values are numbers:

1
2
4
13


Note that if you mix number and words in the list, .sort() will sort numbers in string like order before any words. Using .nsort, any non-numbers are sorted before the numbers which then sort in numerical order.

The output of a sorted list is presumed to be a string, i.e. values concatenated into a single string. Passing a sorted List to another List will do a one-time re-sort of the stored (string) values, but it can still be re-sorted. I repeat there is no 'number list' data type.

For most case ignore the syntax where a an attribute name argument is supplied. This is only used if you want to sort list A using list B's values - which I don't think is where you're trying to do here. It's a while since I wrote that aTbRef article but I think the point of the last part is that if you pass an attribute as an argument, the value(s) and data-type of that attribute trump the default sort/isort/nsort of the specified operator.

Does that help?

Title: Re: Agent Sorting-- Numbers
Post by Mark Anderson on Aug 9th, 2016, 5:50pm

I should add that if you use decimal numbers (e.g. 1.4, 0.325) with or instead of integers (1, 2, 9), .nsort() will sort these correctly.

Not tested, but I assume Tinderbox uses your Mac's locale to figure out number formatting.  For instance, where an English system would use '1.4' for "one point four', a French system will (may?) use '1,4' as a comma is used as the decimal separator. I only tested on my UK (English/British) system. If your Mac uses a non-period as the decimal separator your results may vary. Mark Bernstein may be able to clarify (I can't see under the hood) but I'd expect number-sort of decimals to follow the user's system's locale settings to figure out what is a decimal! I believe Arabic and Persian use a different, sepcific character, as the separator. These things can get complex...  ;)

Title: Re: Agent Sorting-- Numbers
Post by Steve Scott on Aug 9th, 2016, 8:15pm

Mark, as always, thanks for your in-depth help.  It is invaluable.

Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.