|
|
${
/* Use the function tdSortKeys to sort and array of TabularData row ids (keys) by the value of some tabular data column named column */ int[] tdSortKeys(int[] keys, TabularData td, String column) { quicksort(keys,new TDComparator(td,column),0,length(keys)-1); return keys; } class TDComparator { TabularData td; String column; TDComparator(TabularData td, String column){ this.td=td; this.column=column; } int compare(int a,int b){ if(td.getColumnType(column) eq "String") { if(td.getString(a,column) eq td.getString(b,column)) return 0; if(td.getString(a,column) gt td.getString(b,column)) return 1; return -1; } else /* (td.getColumnType(column) eq "int") */ { if(td.getInt(a,column) == td.getInt(b,column)) return 0; if(td.getInt(a,column) < td.getInt(b,column)) return 1; return -1; } } } void quicksort( int[] a, Object co, int low, int high ) { int pivot; IntStack lows = new IntStack(),highs = new IntStack(); lows.push(low); highs.push(high); /* Termination condition! */ while ( lows.size()>0 ) { low = lows.pop(); high = highs.pop(); if(high > low) { int pivot = partition( a,co, low, high ); lows.push(low); highs.push( pivot - 1); lows.push(pivot+1); highs.push(high); } } }
int partition(int a[], Object co,int low, int high ) { TDComparator c = <TDComparator>co; int left, right; int pivot_item; pivot_item = a[low]; left = low; right = high; while ( left < right ) { while(left < right && c.compare(a[left],pivot_item) <= 0 ) left++; while(c.compare(a[right],pivot_item) > 0 ) right--; if ( left < right ) {int t = a[left]; a[left]=a[right]; a[right]=t;} } a[low] = a[right]; a[right] = pivot_item; return right; }
}$
| |
Copyright © 2000 - 2003 David Hakim
|
|