Is there a standard way to compare two strings in vim so that I can quickly determine which string is sorted before the other.
to return 0 if
str1 == str2, 1 if
str1 > str2 and -1 if
str1 < str2
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
str1 < str2 should be enough. From
use 'ignorecase' match case ignore case equal == ==# ==? not equal != !=# !=? greater than > ># >? greater than or equal >= >=# >=? smaller than < <# <? smaller than or equal <= <=# <=? regexp matches =~ =~# =~? regexp doesn't match !~ !~# !~? same instance is is# is? different instance isnot isnot# isnot? Examples: "abc" ==# "Abc" evaluates to 0 "abc" ==? "Abc" evaluates to 1 "abc" == "Abc" evaluates to 1 if 'ignorecase' is set, 0 otherwise ... When comparing two Strings, this is done with strcmp() or stricmp(). This results in the mathematical difference (comparing byte values), not necessarily the alphabetical difference in the local language.
If you want
-1, you can wrap this in a function:
function Strcmp(str1, str2) if a:str1 < a:str2 return -1 elseif a:str1 == a:str2 return 0 else return 1 endif endfunction