Sorting 2D (text) waves, by using one column as a key

Average rating
(4 votes)

This snippet sorts a multicolumn 2D text wave, by using one of the columns as a key (the normal sort operation only sorts 1D waves). The default is alpha numeric sorting, but that's easily fixed by changing the sort options. Also, would not be difficult to modify for a multidimensional numeric sort, just remove all the /t flags.

Function MDtextsort(w,keycol)
	Wave/t w
	variable keycol
 
	variable ii
 
 
	make/t/o/n=(dimsize(w,0)) key
	make/o/n=(dimsize(w,0)) valindex
 
	key[] = w[p][keycol]
	valindex=p
 
	sort/a key,key,valindex
 
	duplicate/o/t w, M_newtoInsert
 
	for(ii=0;ii<dimsize(w,0);ii+=1)
		M_newtoInsert[ii][] = w[valindex[ii]][q]
	endfor
 
	duplicate/o/t M_newtoInsert,w
	killwaves/z key,valindex,M_newtoInsert
End

exactly what I was searching

exactly what I was searching for (the numerical version)

Us, too! We were looking

Us, too! We were looking for the same thing (numerical version). Maybe a critical mass of comments will persuade the WM folks to add a 2D option to the regular Sort operation.....

Back to top