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

Average rating
(0 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

Back to top