Skip to main content
added 271 characters in body
Source Link
azerbajdzan
  • 25.1k
  • 2
  • 22
  • 62
SortBy[list, ToExpression[StringDrop[ToString[#], 1]] &]

Sort also work but needs longer code compared to previous one:

Sort[list, 
 ToExpression[StringDrop[ToString[#1], 1]] < 
   ToExpression[StringDrop[ToString[#2], 1]] &]

On the other hand if you used S[1] instead of S1... then simple Sort would be sufficient:

list = {S[1], S[15], S[19], S[2], S[26], S[37], S[50], S[4], S[8]};
Sort[list]

(* {S[1], S[2], S[4], S[8], S[15], S[19], S[26], S[37], S[50]} *)

Or if you used a more systematic names like S01 instead of S1... then also simple Sort would be sufficient:

list = {S01, S15, S19, S02, S26, S37, S50, S04, S08};
Sort[list]

(* {S01, S02, S04, S08, S15, S19, S26, S37, S50} *)
SortBy[list, ToExpression[StringDrop[ToString[#], 1]] &]

Sort also work but needs longer code compared to previous one:

Sort[list, 
 ToExpression[StringDrop[ToString[#1], 1]] < 
   ToExpression[StringDrop[ToString[#2], 1]] &]
SortBy[list, ToExpression[StringDrop[ToString[#], 1]] &]

Sort also work but needs longer code compared to previous one:

Sort[list, 
 ToExpression[StringDrop[ToString[#1], 1]] < 
   ToExpression[StringDrop[ToString[#2], 1]] &]

On the other hand if you used S[1] instead of S1... then simple Sort would be sufficient:

list = {S[1], S[15], S[19], S[2], S[26], S[37], S[50], S[4], S[8]};
Sort[list]

(* {S[1], S[2], S[4], S[8], S[15], S[19], S[26], S[37], S[50]} *)

Or if you used a more systematic names like S01 instead of S1... then also simple Sort would be sufficient:

list = {S01, S15, S19, S02, S26, S37, S50, S04, S08};
Sort[list]

(* {S01, S02, S04, S08, S15, S19, S26, S37, S50} *)
Source Link
azerbajdzan
  • 25.1k
  • 2
  • 22
  • 62

SortBy[list, ToExpression[StringDrop[ToString[#], 1]] &]

Sort also work but needs longer code compared to previous one:

Sort[list, 
 ToExpression[StringDrop[ToString[#1], 1]] < 
   ToExpression[StringDrop[ToString[#2], 1]] &]