Compare two strings based on ASCII dictionary order

I know the sort builtin function will sort cell arrays of strings in ascii dictionary order. But how may I simply compare two strings to determine which is first (by dictionary order) ? This must be a constituent part of the sort routine, but I cannot find a way to do it. (and sort is builtin so I cannot inspect it).

1 件のコメント

Aravin
Aravin 2012 年 5 月 25 日
Can you give one example ?

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

 採用された回答

Oleg Komarov
Oleg Komarov 2012 年 5 月 25 日

1 投票

[trash,idx] = sort({'abc';'a'})
Then just look at idx(1)

2 件のコメント

Geoff
Geoff 2012 年 5 月 25 日
Oh, that's nice and easy =) I would use [~, idx] though, rather than create a variable called 'trash'.
Brandon Kuczenski
Brandon Kuczenski 2012 年 5 月 25 日
This plus diff(idx) gives a usable result. it does not detect equal strings, however.

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

その他の回答 (2 件)

Geoff
Geoff 2012 年 5 月 25 日

3 投票

Yeah this is a real failing of the strcmp function in my opinion. That function originated in C, and would tell you whether a string was less, equal or greater than another. But it was probably too non-intuitive for MatLab users because it returned zero if the strings were equal.
Basically, what the C function does is subtract the strings character by character. So you can do that here:
function cmp = cstrcmp( a, b )
% Force the strings to equal length
x = char({a;b});
% Subtract one from the other
d = x(1,:) - x(2,:);
% Remove zero entries
d(~d) = [];
if isempty(d)
cmp = 0;
else
cmp = d(1);
end
end
The output is:
a == b : 0
a > b : positive
a < b : negative
There are probably more efficient ways to do this in MatLab. I just stuck to the easy matrix operations.

2 件のコメント

Brandon Kuczenski
Brandon Kuczenski 2012 年 5 月 25 日
char({a;b})- now that is useful. so is ~ instead of Trash; and I like d(~d) = [] as well.. thank you!
Geoff
Geoff 2012 年 5 月 25 日
Haha yeah that logic negation of non-logic values is an old C habit. It's technically not very good programming practice (because it's not as readable as "d==0"), but MatLab does document the behaviour. Use at your own peril =)

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

Junaid
Junaid 2012 年 5 月 25 日

0 投票

Can you give one example. As I understand, you can do it by compare operator.
a = 'abd';
b = 'abc';
a <= b
output is [1 1 0] where 0 indicates that some character in b comes before in by dictionary order.

2 件のコメント

Brandon Kuczenski
Brandon Kuczenski 2012 年 5 月 25 日
That only works if the strings are the same length. In order to do the comparison in general, I have to first test to see which string is longer and either truncate it or pad the other. I suppose that is not too much trouble, but I am surprised that it must be done manually.
Brandon Kuczenski
Brandon Kuczenski 2012 年 5 月 25 日
Also, the single comparison is insufficient to determine which string comes first.
a='hellob'
b='hellbo'
According to a dictionary test, a>b. In order for me to know that, I would need to do both comparisons:
a>=b
>> a>=b
ans =
1 1 1 1 1 0
>> b>=a
ans =
1 1 1 1 0 1
>>
and then see which one has the earlier zero.

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

カテゴリ

ヘルプ センター および File Exchange で Characters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by