Thread Subject: sorting strings using 2 criteria

Subject: sorting strings using 2 criteria

From: Boris

Date: 7 Jan, 2012 01:05:09

Message: 1 of 5

hi all,
suppose i have following set of strings :
   'f13'
    'g12'
    'g13'
    'h12'
    'j12'
    'j13'
    'k12'
    'm12'
    'm13'
    'm15'
    'n12'
    'q12'
    'u12'
    'v12'
    'x12'
    'z12'
    'z13'
    'z14'
    'z15'
    'z16'
    'z17'
    'z19'

i would like to sort them according to following 2 rules:
1. letters should be in this order

 F
G
H
J
K
M
N
Q
U
V
X
Z

2. once letters in above order, numbers should be in order.


do you know what is easiest solution in matlab? thank you in advance,

boris.

Subject: sorting strings using 2 criteria

From: Bruno Luong

Date: 7 Jan, 2012 08:39:08

Message: 2 of 5

s = {'z19';
    'j13';
    'g13';
    'z12';
    'n12';
    'k12';
    'z13';
    'v12';
    'm12';
    'j12';
    'z17';
    'z15';
    'x12';
    'f13';
    'g12';
    'h12';
    'z14';
    'u12';
    'm13';
    'z16';
    'm15';
    'q12'}

% If your data contains fix format of one letter and 2 numbers

sort(s)

% If not adapt this:
letters = cellfun(@(s) s(1), s);
numbers = cellfun(@(s) sscanf(s(2:end), '%d'), s);
[~, is1] = sort(numbers);
[~, is2] = sort(letters(is1));
s(is1(is2))

% Bruno

Subject: sorting strings using 2 criteria

From: Boris

Date: 7 Jan, 2012 15:14:08

Message: 3 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <je90bc$ri9$1@newscl01ah.mathworks.com>...
> s = {'z19';
> 'j13';
> 'g13';
> 'z12';
> 'n12';
> 'k12';
> 'z13';
> 'v12';
> 'm12';
> 'j12';
> 'z17';
> 'z15';
> 'x12';
> 'f13';
> 'g12';
> 'h12';
> 'z14';
> 'u12';
> 'm13';
> 'z16';
> 'm15';
> 'q12'}
>
> % If your data contains fix format of one letter and 2 numbers
>
> sort(s)
>
> % If not adapt this:
> letters = cellfun(@(s) s(1), s);
> numbers = cellfun(@(s) sscanf(s(2:end), '%d'), s);
> [~, is1] = sort(numbers);
> [~, is2] = sort(letters(is1));
> s(is1(is2))
>
> % Bruno


Thank you very much Bruno. It works. I actually wanted to sort at first numbers and after that letters according to rule above, but it is easy to change your code:

.....
[~, is1] = sort(letters);
[~, is2] = sort(numbers(is1));

....

Thanks!
Boris.

Subject: sorting strings using 2 criteria

From: Rune Allnor

Date: 7 Jan, 2012 16:30:03

Message: 4 of 5

On 7 Jan, 09:39, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> s = {'z19';
>     'j13';
>     'g13';
>     'z12';
>     'n12';
>     'k12';
>     'z13';
>     'v12';
>     'm12';
>     'j12';
>     'z17';
>     'z15';
>     'x12';
>     'f13';
>     'g12';
>     'h12';
>     'z14';
>     'u12';
>     'm13';
>     'z16';
>     'm15';
>     'q12'}
>
> % If your data contains fix format of one letter and 2 numbers
>
> sort(s)

I think you mean

sortrows(s)

Rune

Subject: sorting strings using 2 criteria

From: Bruno Luong

Date: 7 Jan, 2012 19:52:08

Message: 5 of 5

Rune Allnor <allnor@tele.ntnu.no> wrote in message <22df9201-8ca9-4b03-a7ad-96186309da11@t30g2000vbx.googlegroups.com>...

> >
> > sort(s)
>
> I think you mean
>
> sortrows(s)
>

I mean sort() (applied on string cell). sortrows() is used for char array.

Bruno

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com