Non-Alphabetical Sorting of Strings

QUESTION: Say, I have a list of names stored in this way:

   namelist = ['Daddy','Groggy','Ally','Curry','Emmy','Bully','Jockey','Hippy','Itchy','Fluffy']

And I have another list of names, which:

  1. May or may not contain all the names in the above list
  2. Is alphabetically sorted by IDL.

But I want to sort this second list in the order in which the names appear in the first, unsorted list. For example: if the sorted list is:

   mylist = ['Emmy','Fluffy','Itchy',Jockey']

I want to reorder mylist compared to the order the names appear in namelist and I also want to reset the indices in my second list to [0,1,2,3] instead of [4,9,8,6].

I've tried couple of different permutations and combinations using STREGEP and such, but I screw up when it comes to resetting indices in the reordered list. (I think pointers is the way to go, but I haven't learned pointers in IDL.)

ANSWER: Heaven forbid you are going to have to learn anything as useful as pointers! No, Greg Michael will show you a method that only requires you to learn about dimensional juggling. Here is Mr. Michael's explanation of the IDL way to do this.

First, create your two lists, and find out how many elements you have in each:

   IDL> namelist = ['Daddy','Groggy','Ally','Curry','Emmy','Bully','Jockey','Hippy','Itchy','Fluffy']
   IDL> nNameList = N_Elements(namelist)
   IDL> mylist = ['Emmy','Fluffy','Itchy','Jockey']
   IDL> nMyList = N_Elements(mylist)

Next, create two integer arrays, each nNameList by nMyList in size, using dimensional juggling. The purpose of the two arrays is to enumerate every possible pairwise combination of the two lists.

   IDL> array_1 = Rebin(Transpose(Indgen(nMyList)), nNameList, nMyList)
   IDL> Print, array_1
       0       0       0       0       0       0       0       0       0       0
       1       1       1       1       1       1       1       1       1       1
       2       2       2       2       2       2       2       2       2       2
       3       3       3       3       3       3       3       3       3       3
   IDL> array_2 = Rebin(Indgen(nNameList), nNameList, nMyList)
   IDL> Print, array_2
       0       1       2       3       4       5       6       7       8       9
       0       1       2       3       4       5       6       7       8       9
       0       1       2       3       4       5       6       7       8       9
       0       1       2       3       4       5       6       7       8       9

There are names matches, where mylist[array_1] is identical to namelist[array_2]. We simply make an array where the matches have a value of 1 and the non-matches have a value of 0. Note that these are string arrays, so case will matter. If the names in mylist are not spelled exactly as they are in namelist, you may have to force the two lists to all uppercase (StrUpCase) or lowercase (StrLowCase) characters before you perform the match.

   IDL> matches = mylist[array_1] EQ namelist[array_2]
   IDL> Print, matches
       0   0   0   0   1   0   0   0   0   0
       0   0   0   0   0   0   0   0   0   1
       0   0   0   0   0   0   0   0   1   0
       0   0   0   0   0   0   1   0   0   0

Your new array will be the namelist array, subscripted by the column indices in which the total of each column in the matches array is greater than 0.

   IDL> indices = WHERE(TOTAL(matches,2) GT 0)
   IDL> Print, indices
           4           6           8           9
   IDL> newarray = namelist[indices]
   IDL> Print, newarray
      Emmy Jockey Itchy Fluffy

Google
 
Web Coyote's Guide to IDL Programming