On Tuesday, June 30, 2020 at 7:19:26 AM UTC-7, Jake wrote:
>
> 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.
>

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

-- 
You received this message because you are subscribed to the Google Groups 
"TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/62da9ab1-4a63-47cb-aa13-f149154572c8o%40googlegroups.com.

Reply via email to