RosettaCodeData/Task/Sort-stability/Phix/sort-stability.phix

26 lines
694 B
Plaintext

sequence test = {{"UK","London"},
{"US","New York"},
{"US","Birmingham"},
{"UK","Birmingham"}}
---------------------
-- probably stable --
---------------------
function cmp(object a, object b)
return compare(a[1],b[1])
end function
test = custom_sort(routine_id("cmp"),test)
pp(test,{pp_Nest,1})
-----------------------
-- guaranteed stable --
-----------------------
function tag_cmp(integer i, integer j)
return compare({test[i][1],i},{test[j][1],j})
-- return compare(test[i][1],test[j][1])
end function
sequence tags = custom_sort(routine_id("tag_cmp"),shuffle(tagset(4)))
for i=1 to length(tags) do
?test[tags[i]]
end for