sorting string based on the value in it

32 views (last 30 days)
Sajid Afaque
Sajid Afaque on 8 Jan 2020
Edited: Walter Roberson on 6 Aug 2024
'temp_25_vgs_4',
'temp_25_vgs_5' ,
'temp_25_vgs_8' ,
'temp_25_vgs_6' ,
'temp_25_vgs_9',
'temp_25_vgs_7'
how do i sort the above in the increasing order of vgs.
how to sort the above kind of strings..i need it in order (
'temp_25_vgs_4'
'temp_25_vgs_5'
'temp_25_vgs_6'
'temp_25_vgs_7'
'temp_25_vgs_8'
'temp_25_vgs_9'
)
  4 Comments
Sajid Afaque
Sajid Afaque on 8 Jan 2020
is their anyway where we can do without using these functions
Stephen23
Stephen23 on 8 Jan 2020
"is their anyway where we can do without using these functions"
Of course: parse the string to split/extract all relevant characters and numbers (e.g. based on a regular expression or isstrprop), convert the numbers to numeric, sort. You can search this forum to find examples of this, the search field is at the top of the page.

Sign in to comment.

Answers (1)

Arjun
Arjun on 6 Aug 2024
Hi,
As per my understanding of the question you want to custom sort a list of strings without using inbuilt functions. I can give you a basic workflow of how to proceed and some dummy code which you can use to construct solution for your application.
Steps:
  1. Preprocess the string.
  2. Manually extract the numeric part from the string after the last _(underscore).
  3. Construct the numeric value.
  4. Write custom sort function such as selection sort or bubble sort to sort the list of strings based on the numeric value extracted.
Here is some dummy code which might be helpful in better understanding of the solution.
% Define the list of variable names
variable_names = {
'temp_25_vgs_44',
'temp_25_vgs_5',
'temp_25_vgs_812',
'temp_25_vgs_6',
'temp_25_vgs_99',
'temp_25_vgs_7'
};
% Function to extract numeric part
function num = extract_vgs_value(str)
% Find the position of the last underscore
underscore_pos = find(str == '_', 1, 'last');
% Extract the numeric part after the last underscore
num_str = str(underscore_pos+1:end);
% Convert to number
num = 0;
for i = 1:length(num_str)
num = num * 10 + (num_str(i) - '0');
end
end
% Implement bubble sort to sort the variable names based on numeric values
n = length(variable_names);
for i = 1:n-1
for j = 1:n-i
if extract_vgs_value(variable_names{j}) > extract_vgs_value(variable_names{j+1})
% Swap the variable names
temp = variable_names{j};
variable_names{j} = variable_names{j+1};
variable_names{j+1} = temp;
end
end
end
% Display the sorted list of variable names
disp('Sorted list of variable names:');
disp(variable_names);
In the code provided above we have used find function from MATLAB whose documentation you can refer in the attached link below.
  1 Comment
Stephen23
Stephen23 on 6 Aug 2024
Edited: Stephen23 on 6 Aug 2024
"Implement bubble sort to sort the variable names based on numeric values"
MATLAB already has a very efficient inbuilt SORT function.
Simpler and more efficient:
C = {'temp_25_vgs_44';'temp_25_vgs_5';'temp_25_vgs_812';'temp_25_vgs_6';'temp_25_vgs_99';'temp_25_vgs_7'}
C = 6x1 cell array
{'temp_25_vgs_44' } {'temp_25_vgs_5' } {'temp_25_vgs_812'} {'temp_25_vgs_6' } {'temp_25_vgs_99' } {'temp_25_vgs_7' }
[~,X] = sort(str2double(extract(C,digitsPattern+textBoundary)));
D = C(X)
D = 6x1 cell array
{'temp_25_vgs_5' } {'temp_25_vgs_6' } {'temp_25_vgs_7' } {'temp_25_vgs_44' } {'temp_25_vgs_99' } {'temp_25_vgs_812'}

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!