Coyote's Guide to IDL Programming

Does Sort Sort?

QUESTION: Excuse me, but I am confused by the following result:

   IDL> Print, stations[0:9]
      S301 S301 S301 S301 S301 S301 S301 S301 S301 S301
   IDL> Print, Sort(stations[0:9])
          5   1   2   3   4   0   6   7   8   9

If the elements of certain part of an array are identical (as they are above), why doesn't IDL generate sort indices in ascending order? This has ugly implications for some programs I am writing. :-(

ANSWER: Yes, this can be a problem. The underlying sort routines IDL uses (which are built into operating system libraries) are not always built to distingish between identical values in the list. Thus, the order in which equal values are reported can be fairly random (as you have discovered).

The solution is to use a better sorting algorithm. Everyone I know uses the BSORT routine from the NASA Astronomy IDL Library, since this gives consistent results on all platforms.

Web Coyote's Guide to IDL Programming