X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 17 Apr 2017 11:20 AM by  MariM
colsort.pro
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
13 Apr 2017 11:32 AM
    On your blog page http://www.harrisgeospati...mn-sort-routine.aspx you reference a column sort routine, and give a link to download it at: ftp://download.exelisvis.com/groups/esg/blog/colsort.pro I am unable to access the site. Help please!

    MariM



    Veteran Member


    Posts:2396
    Veteran Member


    --
    17 Apr 2017 11:20 AM
    It looks like that blog link is broken since it is from 2013. However, I was able to track down the code. You will need to copy and paste this into IDL: and name it colsort.pro:

    ========================================================
    ; docformat = 'rst'
    ;+
    ; :description:
    ; Applies SORT (ascending order) to a selected column of a two-dimensional
    ; array, then applies this sort to all the columns of the array. (It's like
    ; doing a column sort in Excel.)
    ;
    ; :params:
    ; array: in, required, type=numeric
    ; A two-dimensional numeric array.
    ; column_index: in, required, type=integer
    ; The column index over which sorting is performed.
    ;
    ; :keywords:
    ; reverse_sort: in, optional, type=boolean
    ; Set this keyword to do a reverse sort (descending order) on the
    ; elements of the selected column.
    ;
    ; :author:
    ; Mark Piper, ITT VIS, 2009
    ;
    ; :version:
    ; $Id: colsort.pro 182 2009-11-03 21:44:28Z mpiper $
    ;-
    function colsort, array, column_index, reverse_sort=rsort
    compile_opt idl2
    on_error, 2

    info = size(array, /structure)

    ; Test dimensions of input array.
    if info.n_dimensions ne 2 then $
    message, 'Input array must have two dimensions.'

    ; Ensure the sorting index is within a valid range.
    if n_elements(column_index) eq 0 then $
    message, 'Must pass a column index for sorting.'
    n_columns = info.dimensions[0]
    _column_index = (column_index > 0) < (n_columns-1)

    ; Sort the desired column.
    col_sort_index = sort(array[_column_index,*])
    if keyword_set(rsort) then $
    col_sort_index = reverse(col_sort_index)

    ; Apply the sort to each column of the array.
    sorted_array = array
    for i=0, n_columns-1 do $
    sorted_array[i,*] = (array[i,*])[col_sort_index]

    return, sorted_array
    end


    ;+
    ; Example main. Examine the results in the Console.
    ;-
    a = round(randomu(123, 5, 6) * 20.0)
    sort_index = 1
    b = colsort(a, sort_index, /reverse_sort)
    print, 'Original array:'
    print, a
    print
    print, 'Column index to sort on: ', sort_index
    print
    print, 'Sorted (reverse) array:'
    print, b
    end
    You are not authorized to post a reply.