Sort unique string/number column based on last 2 characters

How do I sort a column based on the last 2 characters while maintaining the unique string/number order? I want each group of unique strings/numbers to be reoriganized based on the last 2 characters in this order:
1. ut, fx, os, st, ng (doesn't matter what the order here)
2. ub
3. uu
4. uj
5. nh
Here's parts of the column (it's actually 67x1 cell)
ap12qw_nh
ap12qw_ub
ap12qw_uj
ap12qw_uu
qw54rt_ub
qw54rt_ut
zx23vb_fx
zx23vb_ng
zx23vb_nh
zx23vb_os
zx23vb_st
zx23vb_ub
zx23vb_uj

 采纳的回答

Here is one way:
% The guide to ordering the letter-pairs. (Although the order of the first few letter-pairs
% doesn't matter, it seemed easier to just sort them anyway.)
order_guide = {'ut', 'fx', 'os', 'st', 'ng', 'ub', 'uu', 'uj', 'nh'};
% Input
input = {'ap12qw_nh','ap12qw_ub','ap12qw_uj','ap12qw_uu','zx23vb_fx'}'; % I didn't include them all
% Pull the last two characters of the input
last_two = cellfun(@(x)x(end-1:end),input,'UniformOutput',false);
% Identify which letter-pair the input belongs to, in order
[tf,indexOrder] = ismember(last_two,order_guide);
% Sort the input accordingly
[~,sorting_order] = sort(indexOrder);
sorted_input = input(sorting_order);

3 个评论

Hi thanks for helping. So it sorted it by the last 2 characters. However, the other key part that is missing is keeping the unique first 6 characters together.
For example, with the order_guide = {'ut', 'fx', 'os', 'st', 'ng', 'ub', 'uu', 'uj', 'nh'}; Instead of what is shown here as a result (grouping of last 2 letters - all the ut, fx, os, together),
qw54rt_ut
zx23vb_fx
zx23vb_os
zx23vb_st
zx23vb_ng
ap12qw_ub
qw54rt_ub
zx23vb_ub
ap12qw_uu
ap12qw_uj
zx23vb_uj
ap12qw_nh
zx23vb_nh
I'm trying to sort it while keeping the unique first 6 characters together as group which looks something like this (hope this makes sense):
ap12qw_ub
ap12qw_uu
ap12qw_uj
ap12qw_nh
qw54rt_ut
qw54rt_ub
zx23vb_fx
zx23vb_os
zx23vb_st
zx23vb_ng
zx23vb_ub
zx23vb_uj
zx23vb_nh
% Guide to ordering the 2-letter code.
order_guide = {'ut', 'fx', 'os', 'st', 'ng', 'ub', 'uu', 'uj', 'nh'};
% Input
input = {
'qw54rt_ut';
'zx23vb_fx';
'zx23vb_os';
'zx23vb_st';
'zx23vb_ng';
'ap12qw_ub';
'qw54rt_ub';
'zx23vb_ub';
'ap12qw_uu';
'ap12qw_uj';
'zx23vb_uj';
'ap12qw_nh';
'zx23vb_nh'};
% Pull the first six characters of the input, and find the unique ones
first_six = cellfun(@(x)x(1:6),input,'UniformOutput',false);
unique_first_six = unique(first_six);
% Pull the last two characters of the input
last_two = cellfun(@(x)x(end-1:end),input,'UniformOutput',false);
% Identify which 6-letter code the input belongs to, in order
[~,index6] = ismember(first_six,unique_first_six);
% Identify which 2-letter code the input belongs to, in order
[~,index2] = ismember(last_two,order_guide);
% Sort the input, first according to 6-letter code, then according to 2-letter code
[~,sorting_order] = sortrows([index6 index2],[1 2]);
sorted_input = input(sorting_order);
You are a genius! It works! That was a wall I hit. I couldn't figure it out. Thank you so much! The part you lost me at was using the sortrow command to simulataneously sort first 6 and last 2, but now it makes so much sense

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by