This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
(->> (map vector a b)
(sort-by first)
(apply (partial mapv vector)))
this returns [a b]
the last line is from the 2d- transpose solution
the last line is from the 2d- transpose solution
type sorter struct {
k []K
t []T
}
func (s *sorter) Len() int {
return len(s.k)
}
func (s *sorter) Swap(i, j int) {
s.k[i], s.k[j] = s.k[j], s.k[i]
s.t[i], s.t[j] = s.t[j], s.t[i]
}
func (s *sorter) Less(i, j int) bool {
return s.k[i] < s.k[j]
}
sort.Sort(&sorter{
k: a,
t: b,
})
The custom type sorter implements the 3 methods of sort.Interface.
Swap affects the order of 2 slices at once.
Swap affects the order of 2 slices at once.
record QuickSort<T extends Comparable<T>>(List<T> a, List<?> b) {
void sort(int x, int y) {
if (x >= y || x < 0) return;
int p = f(x, y);
sort(x, p - 1);
sort(p + 1, y);
}
int f(int x, int y) {
T t = a.get(y);
int i, j = i = x;
do if (a.get(j).compareTo(t) < 1) {
swap(a, i, j);
swap(b, i++, j);
} while (++j < y);
swap(a, i, y);
swap(b, i, y);
return i;
}
}
type
TPairedList = class(TStringList)
private
FBuddy: TStrings;
protected
procedure ExchangeItems(Id1, Id2: Integer); override;
public
procedure Sort(Buddy: TStrings); overload;
end;
procedure TPairedList.ExchangeItems(Id1, Id2: Integer);
begin
inherited Exchange(Id1, Id2);
if Assigned(FBuddy) then
FBuddy.Exchange(Id1, Id2);
end;
procedure TPairedList.Sort(Buddy: TStrings);
begin
FBuddy := Buddy;
Sort;
end;
begin
...
a.sort(b);
end.
@keys = qw(x y a f e n);
@vals = qw(1 2 3 4 5 6);
pairmap { push @new_keys, $a; push @new_vals, $b }
unpairs
sort { $a->[0] cmp $b->[0] }
zip \@keys, \@vals;
zip interleaves the two lists into a list of list-pairs. These are sorted using the 0th element of each pair (corresponding to the keys). The list of pairs is flattened by unpairs. Finally, pairmap iterates over the flattened list of pairs and pushes the $a and $b (first and second) element of each pair onto new lists.