Re: How do you sort an array in IDL? [message #11423] |
Mon, 06 April 1998 00:00 |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Robert A. Lematta wrote:
>
> Does anyone know how to sort an array in idl (ala excell) while preserving
> the order of the other columns. For example, if I have a 10 by 10 array
> with the second column being time, how do I sort the entire array by time and
> have the values in the other columns follow. Like you'd do a sort in a
> spreadsheet. Please reply to lematta@spawar.navy.mil. Many thanks in advance.
> I've tried to figure out the sort function but I don't think it works on arraysswith more than one row or column.
>
Robert,
if you are looking for a sort algorithm similar to what you can do
with spreadsheets, you may want to try my w_sort.pro widget (or the
"multisort" procedure therein): this lets you sort an array keyed on one
or several variables (columns). You can find it on my web page, URL
listed below (it will be posted there by tomorrow).
Regards,
Martin.
--
------------------------------------------------------------ -------
Dr. Martin Schultz
Department for Earth&Planetary Sciences, Harvard University
186 Pierce Hall, 29 Oxford St., Cambridge, MA-02138, USA
phone: (617)-496-8318
fax : (617)-495-4551
e-mail: mgs@io.harvard.edu
IDL-homepage: http://www-as.harvard.edu/people/staff/mgs/idl/
------------------------------------------------------------ -------
|
|
|
Re: How do you sort an array in IDL? [message #11484 is a reply to message #11423] |
Thu, 02 April 1998 00:00  |
Robert S. Hill
Messages: 11 Registered: January 1998
|
Junior Member |
|
|
On Thu, 2 Apr 1998, Robert A. Lematta wrote:
> Does anyone know how to sort an array in idl (ala excell) while preserving
> the order of the other columns. For example, if I have a 10 by 10 array
> with the second column being time, how do I sort the entire array by time and
> have the values in the other columns follow. Like you'd do a sort in a
> spreadsheet. Please reply to lematta@spawar.navy.mil. Many thanks in advance.
> I've tried to figure out the sort function but I don't think it works on arraysswith more than one row or column.
You can do it with IDL's clever subscripting syntax:
Set up test data:
IDL> a = reverse(indgen(10))
% Compiled module: REVERSE.
IDL> b=indgen(10)+100
IDL> arr = transpose([[a],[b]])
IDL> print,arr
9 100
8 101
7 102
6 103
5 104
4 105
3 106
2 107
1 108
0 109
Sort on column 0:
IDL> col = arr(0,*)
IDL> s=sort(col)
IDL> arr2 = arr(*,s) ; sort on column, but apply sort to rows
IDL> print,arr2 ; voila
0 109
1 108
2 107
3 106
4 105
5 104
6 103
7 102
8 101
9 100
Bob
--
Robert.S.Hill.1@gsfc.nasa.gov Phone: 301-286-3624
Raytheon STX / Code 681, NASA/GSFC, Greenbelt, MD 20771
|
|
|
Re: How do you sort an array in IDL? [message #11485 is a reply to message #11484] |
Thu, 02 April 1998 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Hi Folks,
> Here is a little example in which a two-column array is
> sorted by the values in the second column, it can
> easily be converted to a function that returns an
> array sorted by any column.
I'm in procrastination mode today. Here is my SortByColumn
function. As you can see, most of it is error checking. :-)
Call it like this:
sortedArray = SortByColumn(array, 3)
Cheers,
David
-----------------------------------------------------------
FUNCTION SortByColumn, array, column
; This function returns a sorted array of the same size and
; type as ARRAY, sorted over the values of the specified COLUMN.
; ARRAY -- 2D array to sort
; COLUMN -- The column to sort over.
On_Error, 1
; Required positional parameters.
IF N_Params() NE 2 THEN $
Message, 'ARRAY and COLUMN parameters are required'
; Get size of the array. Check parameters.
s = Size(array)
IF s[0] NE 2 THEN Message, 'Input array must be 2D. Returning...'
ncolumns = s[1]
IF column GT ncolumns THEN $
Message, "Specified COLUMN doesn't exist. Returning..."
; Sort the data by the specified column.
sortIndex = Sort(array[column,*])
RETURN, array[*,sortIndex]
END
-----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
Re: How do you sort an array in IDL? [message #11486 is a reply to message #11484] |
Thu, 02 April 1998 00:00  |
Liam Gumley
Messages: 473 Registered: November 1994
|
Senior Member |
|
|
Robert A. Lematta wrote:
> Does anyone know how to sort an array in idl (ala excell) while preserving
> the order of the other columns. For example, if I have a 10 by 10 array
> with the second column being time, how do I sort the entire array by time and
> have the values in the other columns follow. Like you'd do a sort in a
> spreadsheet. Please reply to lematta@spawar.navy.mil. Many thanks in advance.
> I've tried to figure out the sort function but I don't think it works on > arrays with more than one row or column.
How about this:
a=dist(10)
print,a,format='(10f6.3)'
idx=sort(a(1,*))
b=a(*,idx)
print,b,format='(10f6.3)'
Cheers,
Liam.
|
|
|
Re: How do you sort an array in IDL? [message #11487 is a reply to message #11484] |
Thu, 02 April 1998 00:00  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Robert A. Lematta (lematta@nosc.mil) writes:
> Does anyone know how to sort an array in idl (ala excell) while preserving
> the order of the other columns.
Here is a little example in which a two-column array is
sorted by the values in the second column, it can
easily be converted to a function that returns an
array sorted by any column.
Cheers,
David
-----------------------------------------------------------
PRO SortColumn
; Create example data.
array = FltArr(2, 10)
array[0,*] = IndGen(10) + 1 ; Number
array[1,*] = RandomU(seed, 10) * 10 ; Value
Print, 'Unsorted Array---'
Print, array
Print, ''
; Sort the data by value.
sortIndex = Sort(array[1,*])
sortedArray = FltArr(2,10)
FOR j=0,1 DO sortedArray[j,*] = (array[j,*])[sortIndex]
Print, 'Array Sorted by Value---'
Print, sortedArray
Print, ''
END
-----------------------------------------------------------
David Fanning, Ph.D.
Fanning Software Consulting
E-Mail: davidf@dfanning.com
Phone: 970-221-0438
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|