From: John Stone (johns_at_ks.uiuc.edu)
Date: Mon Nov 16 2020 - 13:18:19 CST

Hi,
  Here's a trivial example that will sort a list of this type.
It's not the fastest way to do such a sort, but it's easy
to understand, and people can use this as an example of how to use
the Tcl split, lindex, and lsort commands to do their own sorting criteria
for things like this. I've added some (commented out) "puts" commands
so that people can see what is going on in there.

compresnum { res1 res2 } {
# puts "compare:"
# puts " res1: $res1 res2: $res2"

  set t1 [lindex [split $res1 "_"] 1]
  set t2 [lindex [split $res2 "_"] 1]

  set idx1 [expr int($t1)]
  set idx2 [expr int($t2)]

# puts " idx1: '$idx1' idx2: '$idx2'"

  if { $idx1 < $idx2 } {
# puts " less than..."
    return -1
  } elseif { $idx1 > $idx2 } {
# puts " greater than..."
    return 1
  }
  
# puts " equal to..."
  return 0 ;# else return 0 if equal
}

set reslist { \
      GLU_43 GLY_8 VAL_11 ILE_13 TYR_15 LYS_16 VAL_18 LYS_36 ASP_19 LEU_20 \
      SER_21 LYS_22 VAL_23 SER_25 LYS_26 GLY_7 CYS_27 VAL_14 GLY_28 THR_24 \
      SER_29 LEU_30 GLY_31 GLY_9 ASN_32 GLN_12 ILE_33 HIE_34 HIE_35 PRO_37 \
      GLY_38 GLY_39 PRO_17 GLY_40 GLN_41 VAL_42 PRO_6 LYS_10 \
    }

set newlist [lsort -command compresnum $reslist]

puts "new list: $newlist"

On Mon, Nov 16, 2020 at 08:47:14AM -0500, Axel Kohlmeyer wrote:
> On Mon, Nov 16, 2020 at 6:54 AM Prathit Chatterjee
> <[1]pc20apr_at_remove_yahoo.co.in> wrote:
>
> Dear VMD/TCL experts,
> Kindly let me know how to sort the following list
>
> "GLU_43 GLY_8 VAL_11 ILE_13 TYR_15 LYS_16 VAL_18 LYS_36 ASP_19 LEU_20
> SER_21 LYS_22 VAL_23 SER_25 LYS_26 GLY_7 CYS_27 VAL_14 GLY_28 THR_24
> SER_29 LEU_30 GLY_31 GLY_9 ASN_32 GLN_12 ILE_33 HIE_34 HIE_35 PRO_37
> GLY_38 GLY_39 PRO_17 GLY_40 GLN_41 VAL_42 PRO_6 LYS_10"
>
> as an increasing function of the integers in the corresponding suffices
> as given below:
>
> PRO_6 GLY_7 GLY_8 GLY_9 LYS_10 VAL_11 GLN_12 ILE_13 VAL_14 TYR_15 LYS_16
> PRO_17 VAL_18 ASP_19 LEU_20 SER_21 LYS_22 VAL_23 THR_24 SER_25 LYS_26
> CYS_27 GLY_28 SER_29 LEU_30 GLY_31 ASN_32 ILE_33 HIE_34 HIE_35 LYS_36
> PRO_37 GLY_38 GLY_39 GLY_40 GLN_41 VAL_42 GLU_43
>
> I tried different options related to the "lsort" command, but could not
> resolve the issue.
>
> I am not aware of any list sorting implementation that can do a sort based
> on numerical value with an offset into a string.
> Thus what you need to do is to first transform your list into something
> that can be sorted, e.g. split each string into a sublist (GLU_43 ->
> {"GLU" "43"}) and then you can use the -index option to test lsort to sort
> by the index into the sublist. you may also want to use the -integer
> option to sort by numerical value and not string value.
> Axel.
> Â
>
> Any suggestions will be deeply appreciated.
> Thanks in advance,
> Prathit
>
> --
> Dr. Axel Kohlmeyer  [2]akohlmey_at_gmail.com  [3]http://goo.gl/1wk0
> College of Science & Technology, Temple University, Philadelphia PA, USA
> International Centre for Theoretical Physics, Trieste. Italy.
>
> References
>
> Visible links
> 1. mailto:pc20apr_at_remove_yahoo.co.in
> 2. mailto:akohlmey_at_gmail.com
> 3. http://goo.gl/1wk0

-- 
NIH Center for Macromolecular Modeling and Bioinformatics
Beckman Institute for Advanced Science and Technology
University of Illinois, 405 N. Mathews Ave, Urbana, IL 61801
http://www.ks.uiuc.edu/~johns/           Phone: 217-244-3349
http://www.ks.uiuc.edu/Research/vmd/