you can adapt from this one :
I asking for sorting table not the fromatting of text.
yep.
so you have the function to extract from your notation (RkgM) the numerical values, which can then be sorted just normally.
- Create your own sort list first
(Tools → Options → LibreOffice Calc → Sort Lists)
- Then create an auxiliary column, extract the letters and sort by them.
enable natural sort need to be checked too.
This works. But is it possible without another column?
you have this other option, which would require then to actually enter your data in numerical form :
and for reference : 36864 – Calc cell format for Bytes
Probably not.
Calc will not directly interpret the component value encoding as numeric, so some way or other you need to convert to actual numerical value or some entity which sorts equivalently.
I attached a file showing how I might do it. Three extra columns, no less. You could make that a one-liner (so you’d only need one extra column), but that would be fairly convoluted I guess.
Also with multiple encodings I might use a lookup table instead of the inline string and the CHOOSE function.
if you need to do calculations on your electronics projects, the actual values are also useful. The table can be used for lookups.
The second sheet is added for robustness, in case your decimal separator is not the same as mine. If you don’t need to cater for different locales, you can simplify a bit.
ResistorEncoding.ods (11.7 KB)
so far [PKG] solution is best for now. Maybe if I reformat input column to decimal (ie 2.7k instead 2k7) is it possible to use standard (decimal) prefixes to do sort?
Struggling for single-column solutions isn’t necessary. Sheets have enough columns to give room for a well structured and maintainable solution. You easily can hide these columns - and show them again as soon as you need them.
See:
ResistanceMess.ods (27.0 KB)
…
When I first found this funny way to list resistors with their values in that messed-up notation 50 years ago I thought of a bad joke which soon would be over. Wrong! Now I know that rarely something is as log-lived as specially bad ideas.
- Standard prefixes are prefixes to SI units.
- They are not standardized as postfixes to numbers.
- No they are not supported by a default feature.
- If you need them often it may pay to write custom code for the interpretation.
- There are no means to get them into a number format described by an ordinary code.
It gives error prevention for physical components that are readily marked. A decimal point (or comma) is easily missed or scraped off or inserted by a speck of dirt.
Of course it isn’t the only method, the coloured rings on small components don’t use characters at all
Neither method was intended for cataloguing, although for untrained people a list of the actual markings make stocktake and fulfilling orders less error prone.
If using additional columns then probably simplest way should be to convert funky notation to decimal value and then sort that.
So this is my solution.
REGEX to replace any letter into decimal.
multiply by 1000 if k is found or multiply by 1000000 if M is found, otherwise multiply by 1 if not K or M.
I just wonder if formula can be even shorter. (I’m really ocassional user of libreoffice)
current formula is:
=REGEX(A2,"\D",".","g")*IF(IFERROR(FIND("k",A2),0),1000, IF(IFERROR(FIND("M",A2),0),1000000,1))
can have only 1 letter ? so "g"
is superfluous.
too short since “G” 1000000000 is missing
this could almost make it :
=1000^SEARCH( REGEX(A2, "\d","", "g"), "KMG", 1)
then
And one line/column:
=(REGEX(A2,"\D",".")) *(10^(3*(FIND(REGEX(A2,"\d","","g"),"RkMG",1)-1)))
I replaced SEARCH with FIND to be case sensitive. So any letter other than RkMG show error.
Thanks for help
This version works with any locale and with restrictive settings on formula syntax: =NUMBERVALUE(REGEX(A1;"\D";".");".") *NUMBERVALUE((10^(3*(FIND(REGEX(A1;"\d";"";"g");"RkMG";1)-1)));".")
or this:
=NUMBERVALUE(REGEX(A1;"\D";".");".")*1000^(FIND(REGEX(A1;"\D");"RkMG")-1)
formula for B1:
=1*SUBSTITUTE(A1;REGEX(A1;"[RkMG]");VLOOKUP(REGEX(A1;"[RkMG]");$G$1:$H$4;2;0))
Content of G1:H4
R | , |
---|---|
k | 000, |
M | 000000, |
G | 000000000, |
resistor_sort.ods (12.4 KB)
in case your use decimalpoint instead decimalcomma change Column H to your needs
bonus: Direct Sort of CurrentSelection by Resistor_syntax in python:
edit: corrections in python
import re
rex = re.compile(r'([RkMG])')
def repl(resistor):
value, unit, decimals = rex.split(resistor[0])
return ("RkMG".index(unit), float(f"{value}.{decimals}"))
def sort_resistors():
sel = XSCRIPTCONTEXT.getDocument().CurrentSelection
resistors = sel.DataArray
sel.DataArray = sorted(resistors, key=repl)
Without auxiliary ranges and taking into account different fraction separators.
=VALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1; "R"; "|"); "k"; "000|");"M";"000000|") ;"G";"000000000|");"|";MID(3/2;2;1)))
This solution is way shorter and probably easy to understand
=(REGEX(A2,"\D",".")) *(10^(3*(FIND(REGEX(A2,"\d","","g"),"RkMG",1)-1)))