How can i compare two strings by their alphabetical order

12 ビュー (過去 30 日間)
Denememe
Denememe 2016 年 12 月 1 日
回答済み: Paul Herselman 2024 年 1 月 25 日
I want to write a function which will compare two strings by their alphabetical order. The function should give an output '-1' if the first string is 'a' and second is 'b'. I couldn't do that with strcmp Thank you
  2 件のコメント
James Tursa
James Tursa 2016 年 12 月 1 日
Please give a few more examples so we are sure what your function is supposed to do.
Denememe
Denememe 2016 年 12 月 1 日
function output = strCompare(s1,s2)
ls1=length(s1);
ls2=length(s2);
if ls1<ls2
for i=1:ls1
if s1(i)<s2(i)
output = [-1];
elseif s1(i)>s2(i)
output = [1];
else
output = [0];
end
end
elseif ls1>=ls2
for i=1:ls2
if s1(i)<s2(i)
output = [-1];
elseif s1(i)>s2(i)
output = [1];
else
output = [0];
end
end
end
end
I wrote this function but i want to have an output '1' when s1 = 'America' and s2='A' I take '0' when i do that

サインインしてコメントする。

回答 (3 件)

dpb
dpb 2016 年 12 月 1 日
編集済み: Walter Roberson 2016 年 12 月 2 日
>> 'a'-'b'
ans =
-1
>>
may give you some ideas??? Or, instead of reinventing the wheel,
lexcmp from File Exchange probably does what you're looking for...

Jomar Bueyes
Jomar Bueyes 2019 年 8 月 23 日
編集済み: Jomar Bueyes 2019 年 8 月 23 日
I know this question is old. But I wrote a the function below that does what Denememe wants.
function seg = arraySortCriterion(a, b)
% arraySortCriterion returns -1, 0, 1 depending of how a,b must be sorted
%
% SYNTAX:
% seg = arraySortCriterion(a, b);
%
% DESCRIPTION:
% seg = arraySortCriterion(a, b);
% Returns;
% -1 if a must precede b when sorted
% 1 if b must precede a when sorted
% 0 if a and b can be in either order
%
% For arrays and strings, the function compares element by element
% until it finds an element such that a(k) ~= b(k) and returns
% -1 if a(k) < b(k)
% 1 if a(k) > b(k)
% If the two strings or arrays are equal up to the length of the
% shortest one, the shorter array or string goes first in when
% sorted. This is consistent with how Matlab, Python, and Perl sort
% this of strings of different lengths that are equal up to the
% length of the shortest one.
% Author/Date
% Jomar Bueyes
%
% Copyright 2019 Jomar Bueyes
Na = numel(a);
Nb = numel(b);
for k = 1:min(Na, Nb)
seg = sign(a(k) - b(k));
if seg ~= 0
return;
end
end
% ..The two strings/arrays are equal up to the length of the shortest one
seg = sign(Na-Nb); % This places the shortest one first.
return
end

Paul Herselman
Paul Herselman 2024 年 1 月 25 日
Here is another alternative, using MatLabs convertCharsToStrings function
convertCharsToStrings('abc') < convertCharsToStrings('def')
or if you want a function:
function [lexicalOrder] = strCmpLikeC(str1, str2)
%[lexicalOrder] = strCmpLikeC(str1, str2)
% a strcmp function like the c language
% inputs: str1 and str2 can be chars or strings
% return 0 if strings match
% return 1 if first non matching character of str1 is lexically greater (in Ascii) than corresponding character in str2
% else return -1
aStr = convertCharsToStrings(str1);
bStr = convertCharsToStrings(str2);
lexicalOrder = 0;
if aStr > bStr
lexicalOrder = 1;
elseif bStr > aStr
lexicalOrder = -1;
end
end

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by