Ruby 81 bytes
m=Score.each_index.sort_by{|i|Score[i]}
p m.map{|i|Names[i]}
p m.map{|i|Email[i]}
A golfed implemention of the algorithm below, assuming Score
, Names
, and Email
are all already defined, and that only output of the post-sorted Names
and Email
is required.
Sample usage:
Score = ['score1','score10','score6','score2','score22','score12']
Names = ['name1','name10','name6','name2','name22','name12']
Email = ['email1','email10','email6','email2','email22','email12']
m=Score.each_index.sort_by{|i|Score[i]}
p m.map{|i|Names[i]}
p m.map{|i|Email[i]}
Sample output:
["name1", "name10", "name12", "name2", "name22", "name6"]
["email1", "email10", "email12", "email2", "email22", "email6"]
Ruby (with natural sort) 123 bytes
m=Score.each_index.sort_by{|i|Score[i].scan(/\d+|\D+/).map{|i|i=~/\d/?i.to_i: i}}
p m.map{|i|Names[i]}
p m.map{|i|Email[i]}
Correctly sorts strings with any number of digital groupings, as one would sort them 'naturally'. The only caveat is that all strings in the Score
array must start with a digit, or all must start with a non-digit.
Sample output:
["name1", "name2", "name6", "name10", "name12", "name22"]
["email1", "email2", "email6", "email10", "email12", "email22"]
Python
Not a code-golf answer.
- Find the inverse permutation of the list which is to be sorted in lexicographical order (in the problem description, this
Scores
array). This can be achieved in O(n log n) time (as the problem is analog to sorting), although the implementation below is O(n²).
- Apply this permutation to each list. The permute function is O(n) complexity.
Sample implementation:
def get_inv_perm(items):
size = len(items)
perm = [0]*size
for i in range(size):
for j in range(size): perm[j] += items[j] > items[i]
return perm
def permute(arr, perm):
size = len(arr)
out = [0] * size
for i in range(size):
val = perm[i]
out[i] = arr[val]
return out
if __name__ == "__main__":
Score = ['score1','score10','score6','score2','score22','score12']
Names = ['name1','name10','name6','name2','name22','name12']
Email = ['email1','email10','email6','email2','email22','email12']
perm = get_inv_perm(Score)
print permute(Score, perm)
print permute(Names, perm)
print permute(Email, perm)
Sample output:
['score1', 'score10', 'score12', 'score2', 'score22', 'score6']
['name1', 'name10', 'name12', 'name2', 'name22', 'name6']
['email1', 'email10', 'email12', 'email2', 'email22', 'email6']
Because the sort is lexicographical, score10
and score12
appear directly after score1
. The get_inv_perm
function assumes the items list is unique. The remaining lists need not be.
ScoreSorted
to be input, output, or neither. – Peter Taylor Jan 6 at 10:26A
, and a listA'
which is a permutation ofA
, and then for a given listB
we should find the same permutationB'
? – marinus Jan 6 at 13:31A
with a logical lexicographical ordering, sort this list, and apply the same permutation to bothB
andC
. This is analogous to sorting data rows in a spreadsheet by a specific column. – primo Jan 7 at 4:43