Suppose you want to sort a list L ( of numbers ) and also determine order of elements as a list of indices corresponding to elements of original list L, that is, you want such a integer list "I" that "seq(L[I[j],j=1..nops(L))" to be equivalent to sorted list. This functionality is present in MATLAB in "[B,IX] = sort(...)" syntax and i come up with this problem while trying to convert a MATLAB function (GaussQuadratureWeights) to maple. The procedure described resolves problem using a few MAPLE commands including MAPLE's built-in sort function. IndexedSort := proc(numbers) local indices,numbersAndIndices,sortFunction,sortResult,SortedValues,SortedIndices; # create list of indices indices := [seq(i,i=1..nops(numbers))]; # zip indices with list to be sorted (1st element=index, 2nd element=value) numbersAndIndices := zip( (x,y) -> [x,y], indices, numbers); # set sort criteria to be comparison on second value of 2-tuple sortFunction := (a,b) -> a[2] < b[2]; # apply MAPLE's built-in sort function sortResult := sort( numbersAndIndices, sortFunction); # extract indices and sorted values back from "sortResult" SortedIndices := map( x -> x[1], sortResult); SortedValues := map(x -> x[2], sortResult); # return both as tuple RETURN(SortedValues,SortedIndices); end; Testing procedure: > L := [4,5,3]; L := [4, 5, 3] > Values, Indices := IndexedSort(L); Values, Indices := [3, 4, 5], [3, 1, 2] > seq(L[Indices[j]],j=1..nops(Indices)); 3, 4, 5

Please Wait...