# Sort rows of a table by column within a variable

조회 수: 5 (최근 30일)
J AI 2020년 7월 28일
답변: Eric Sofen 2024년 3월 12일
I have a table:
T =
a v
________ _______________________________
0.88517 0.33536 0.65376 0.89092
0.91329 0.67973 0.49417 0.33416
0.79618 0.13655 0.77905 0.69875
0.098712 0.72123 0.71504 0.19781
0.26187 0.10676 0.90372 0.030541
I want to sort the table using the last column of v. This is the closest I could find but obviously it does not work (thus I am posting the question):
sortedT = sortrows(T,'v')
I am not sure how it sorts in this case:
sortedT =
a v
________ _______________________________
0.26187 0.10676 0.90372 0.030541
0.79618 0.13655 0.77905 0.69875
0.88517 0.33536 0.65376 0.89092
0.91329 0.67973 0.49417 0.33416
0.098712 0.72123 0.71504 0.19781
I also looked up in sortrow documentation but could not find anything there (did I slip?).
Your help is highly appreciated! Thank you.
##### 댓글 수: 2없음 표시없음 숨기기
BN 2020년 7월 28일
I think it could be done if you convert your table to matrix then sort it as you like and the return it to table. Jus an idea...
J AI 2020년 7월 28일
Yes, I suppose I could do that. I have actually achieved what I was trying to achieve in a different way already. So, just as a learning process, let's see if there is a direct way we can achieve this.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Harsha Priya Daggubati 2020년 7월 30일
Hi,
As you can see from the data, whenever you apply sortrows to your table, by giving your column name as 'v' data will be sorted based on the first column of your merged column 'v'.
As a workaround, you can use 'splitvars' method to update your table and apply sortrows, then later merge your table which you need as a final result using 'mergevars'.
Here is the code to it:
a = [0.88517 ;0.91329;0.79618;0.098712;0.26187];
v = [ 0.33536 0.65376 0.89092; 0.67973 0.49417 0.33416; 0.13655 0.77905 0.69875; 0.72123 0.71504 0.19781; 0.10676 0.90372 0.030541];
tbl = table(a,v)
tbl1 = splitvars(tbl);
tbl1 = sortrows(tbl1,[4]);
finaltbl = mergevars(tbl1,[2 3 4],'NewVariableName','v');
Hope this helps!

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Eric Sofen 2024년 3월 12일
There isn't a one-line solution to this, but here's another approach that hoists the data you want to sort by out of the table and then uses the sort index (second output of sortrows) to sort the table itself:
% Setup
t.BloodPressure = [t.Systolic, t.Diastolic];
t = removevars(t,["Systolic","Diastolic"]);
% Sort the second column of the BloodPressure Variable to get the sort order
[~,i] = sortrows(t.BloodPressure(:,2));
% Index back into t with the sort order.
tsorted = t(i,:)
tsorted = 100×9 table
LastName Gender Age Location Height Weight Smoker SelfAssessedHealthStatus BloodPressure __________ ________ ___ ___________________________ ______ ______ ______ ________________________ _____________ "Thomas" "Female" 42 "St. Mary's Medical Center" 66 137 false "Poor" 115 68 "Davis" "Female" 46 "St. Mary's Medical Center" 68 142 false "Good" 121 70 "Cooper" "Female" 28 "VA Hospital" 65 127 false "Good" 115 73 "Bryant" "Female" 48 "County General Hospital" 66 134 false "Excellent" 129 73 "Jackson" "Male" 25 "VA Hospital" 71 174 false "Poor" 127 74 "Carter" "Female" 38 "St. Mary's Medical Center" 63 128 false "Good" 120 74 "Morgan" "Female" 41 "St. Mary's Medical Center" 66 134 false "Good" 120 74 "Brooks" "Male" 39 "St. Mary's Medical Center" 72 176 false "Excellent" 120 74 "Griffin" "Male" 49 "County General Hospital" 70 186 false "Fair" 119 74 "Jones" "Female" 40 "VA Hospital" 67 133 false "Fair" 117 75 "Clark" "Female" 48 "VA Hospital" 65 133 false "Excellent" 121 75 "James" "Male" 25 "County General Hospital" 66 186 false "Good" 125 75 "Bennett" "Female" 35 "County General Hospital" 64 131 false "Fair" 121 75 "Robinson" "Male" 50 "County General Hospital" 68 172 false "Good" 125 76 "Young" "Female" 25 "County General Hospital" 63 114 false "Good" 125 76 "Evans" "Female" 39 "County General Hospital" 62 121 false "Good" 123 76

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by