//Demonstrates Array sorting functions //E.g. useful vor finding the position of the maximum in a profile //N. Vischer, 12-Nov-2010 macro "Sort Numbers [F1]"{ theArray = newArray(333, 22, 6666, 8); doSort(theArray); } macro "Sort Strings [F2]"{ theArray = newArray("BB", "a", "ccc", "", "DDDD"); doSort(theArray); } function doSort(theArray){ print ("\\Clear"); sortedValues = Array.copy(theArray); Array.sort(sortedValues); rankPosArr = Array.rankPositions(theArray); ranks = Array.rankPositions(rankPosArr); print ("Original array:"); for (jj = 0; jj < theArray.length; jj++){ print(jj, ": ", theArray[jj]); } print ("\nSorted array (starting with smallest value):"); for (jj = 0; jj < theArray.length; jj++){ print(sortedValues[jj]); } print ("\nRank Positions (starting with index of smallest value):"); for (jj = 0; jj < theArray.length; jj++){ print(rankPosArr[jj]); } print ("\nRanks (starting with rank of first value):"); for (jj = 0; jj < theArray.length; jj++){ print(ranks[jj]); } print ("\n- Smallest value is defined to have rank zero"); print ("- Use Array.invert to change between ascending and descending order."); print ("- String sorting ignores case."); }