1// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5//go:build !go1.2
6// +build !go1.2
7
8package language
9
10import "sort"
11
12func sortStable(s sort.Interface) {
13	ss := stableSort{
14		s:   s,
15		pos: make([]int, s.Len()),
16	}
17	for i := range ss.pos {
18		ss.pos[i] = i
19	}
20	sort.Sort(&ss)
21}
22
23type stableSort struct {
24	s   sort.Interface
25	pos []int
26}
27
28func (s *stableSort) Len() int {
29	return len(s.pos)
30}
31
32func (s *stableSort) Less(i, j int) bool {
33	return s.s.Less(i, j) || !s.s.Less(j, i) && s.pos[i] < s.pos[j]
34}
35
36func (s *stableSort) Swap(i, j int) {
37	s.s.Swap(i, j)
38	s.pos[i], s.pos[j] = s.pos[j], s.pos[i]
39}
40