Show part of field value

44 views
Skip to first unread message

Jake

unread,
Jun 30, 2020, 10:19:26 AM6/30/20
to TiddlyWiki
A little question. I wanted to sort projects by projectsize. But as I found out TW sorts them based on "text" logic, not "math" logic. So, if I want 140.15 Gb be higher then 1.3 Gb, 2 Gb, 3 Gb and not between then I need to give them values 001.5 Gb, 002 Gb, 003 Gb. But I don't need those additional "zeros" to show in the appropriate part of the table. So, the question is how to "remove" those? I thought "removeprefix" should have worked, but if I try this:

<$text text={{{[{!!projectsize}removeprefix[00]]}}} />

and the field value has "00" in the beginning - everything is ok, but if it doesn't - I get empty result. I think I messed up with syntax somewhere, but I don't know where. I tried " +removeprefix", but that also didn't work out. Why doesn't it remove "zeroes" where they are and leave the rest as it is?

Plus if there is a single "0" before a number, e.g.: 015.43 Gb, i want to remove that as well.

Eric Shulman

unread,
Jun 30, 2020, 11:22:05 AM6/30/20
to TiddlyWiki
removeprefix[foo] means almost the same thing as prefix[foo]: it matches items that actually have a prefix of "foo".
The difference is that removeprefix[] then also removes the "foo" prefix, leaving the remaining text behind.

For your stated use, where the !!projectsize  contains numbers that are "zero-padded", you can write something like this:
<$text text={{{ [{!!projectsize}removeprefix[00]] ~[{!!projectsize}removeprefix[0]] ~[{!!projectsize}] }}} />
The leading ~ before each filter expression means "if the filter results so far are empty"

Thus, if the project size has leading "00", the first filter expression returns a result,
but if it has leading "0", the second filter expression returns a result, and
and if neither of those are matches, then it just gives the project size "as is".

However, there may be another way to address this.

Let's suppose you *don't* store any leading zeros in the projectsize field values.
Then, the problem is, as you originally noted, is that the sort[] filter does a *text* sort,
so that "1.3" and "140.15" both come before "2" and "3"

However, the nsort[] filter does a *numeric* sort, so that the results (using your example values)
would be ordered as: 1.3, 2, 3, and 140.15, as you intended.

One minor problem arises because your fields also include a suffix of " Gb", which makes the
field values "non-numeric" and, as written here: https://tiddlywiki.com/#nsort%20Operator
"Non-numeric values are treated as having a higher value than any number" 
Which automatically defeats the numeric sorting and reverts to text sort order.

To address this, you would first have to omit that text from the projectsize field of each
project tiddler.  Then, to show a table of project titles and sizes, sorted numerically by projectsize,
you could write something like this (assuming you have tiddlers tagged with "project"):
<table>
  <$list filter="[tag[project]nsort[projectsize]]">
     
<tr><td> {{!!title}}</td><td>{{!!projectsize}}</td></tr>
   </$list>
</table>

Let me know how it goes...

enjoy,
-e

Jake

unread,
Jun 30, 2020, 12:18:02 PM6/30/20
to TiddlyWiki
One minor problem arises because your fields also include a suffix of " Gb", which makes the
field values "non-numeric" and, as written here: https://tiddlywiki.com/#nsort%20Operator
"Non-numeric values are treated as having a higher value than any number" 
Which automatically defeats the numeric sorting and reverts to text sort order.

No-no-no, I'm dumb, but not THAT dumb. :) Of course "Gb" was outside the field value. I wouldn't expect it to handle numeric+text field as numeric value. 


 However, the nsort[] filter does a *numeric* sort, so that the results (using your example values)
would be ordered as: 1.3, 2, 3, and 140.15, as you intended.


To address this, you would first have to omit that text from the projectsize field of each
project tiddler.  Then, to show a table of project titles and sizes, sorted numerically by projectsize,
you could write something like this (assuming you have tiddlers tagged with "project"):
<table>
  <$list filter="[tag[project]nsort[projectsize]]">
     
<tr><td> {{!!title}}</td><td>{{!!projectsize}}</td></tr>
   </$list>
</table>


Yeah, I had smth like this (though it was "sort" instead of "nsort" - silly me, and it worked fine untill I got to difference in digits :) )
<$list filter="[tag[project]has[gamesize]!nsort[gamesize]]"> <ul> <li> <$link/> <red>(<$view field="projectsize" /> Gb)</red> <$transclude tiddler={{!!rating-image}} /> </li> </ul>  </$list>
 

 Let me know how it goes...

Yay! Both ways worked fine! Both the ~filter and nsort. Of course I'll stick with "nsort" as the most obvious choice. :) Though I didn't know about that "tilda" thingy and specificity of "removeprefix" operator mechanics - might come in handy later for other purpose.

Thanks a lot, Eric!
Reply all
Reply to author
Forward
0 new messages