[Go]

[Go] golang sort.Slice를 사용할때의 주의점 sort.SliceStable와의 차이점 (Important Considerations When Using golang sort.Slice and Differences from sort.SliceStable)

dogfootman 2023. 7. 27. 23:56

개요 

sort.Slice를 사용했을때와 sort.SliceStable를 사용했을때의 결과를 비교

 

 

1. sort.Slice

package main

import (
	"fmt"
	"sort"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	people := []Person{
		{"Alice", 30},
		{"Bob", 30},
		{"Charlie", 30},
		{"David", 30},
		{"Eve", 30},
		{"Alice", 10},
		{"Bob", 10},
		{"Charlie", 10},
		{"David", 10},
		{"Eve", 10},
		{"Alice", 20},
		{"Bob", 20},
		{"Charlie", 20},
		{"David", 20},
		{"Eve", 20},
	}

	// 이름으로 정렬
	sort.Slice(people, func(i, j int) bool {
		return people[i].Name < people[j].Name
	})
	fmt.Println("이름으로 정렬 sort.Slice:")
	for _, p := range people {
		fmt.Printf("%s - Age: %d\n", p.Name, p.Age)
	}

	fmt.Printf("\n")

	// 나이로 정렬
	sort.SliceStable(people, func(i, j int) bool {
		return people[i].Age < people[j].Age
	})
	fmt.Println("나이로 정렬 sort.Slice:")
	for _, p := range people {
		fmt.Printf("%s - Age: %d\n", p.Name, p.Age)
	}
}

결과값:

이름으로 정렬 sort.Slice:
Alice - Age: 20        
Alice - Age: 30        
Alice - Age: 10        
Bob - Age: 30
Bob - Age: 10
Bob - Age: 20
Charlie - Age: 30      
Charlie - Age: 20      
Charlie - Age: 10      
David - Age: 30        
David - Age: 10        
David - Age: 20        
Eve - Age: 10
Eve - Age: 30
Eve - Age: 20

나이로 정렬 sort.Slice:
David - Age: 10        
Alice - Age: 10        
Eve - Age: 10
Bob - Age: 10
Charlie - Age: 10
Eve - Age: 20
Bob - Age: 20
Alice - Age: 20
Charlie - Age: 20
David - Age: 20
Charlie - Age: 30
David - Age: 30
Bob - Age: 30
Eve - Age: 30
Alice - Age: 30

퀵소트를 베이스로 하는 정렬로 슬라이스의 원래의 순서가 보장 되지않는다

 

 

2. sort.SliceStable

package main

import (
	"fmt"
	"sort"
)

type Person struct {
	Name string
	Age  int
}

func main() {
	people := []Person{
		{"Alice", 30},
		{"Bob", 30},
		{"Charlie", 30},
		{"David", 30},
		{"Eve", 30},
		{"Alice", 10},
		{"Bob", 10},
		{"Charlie", 10},
		{"David", 10},
		{"Eve", 10},
		{"Alice", 20},
		{"Bob", 20},
		{"Charlie", 20},
		{"David", 20},
		{"Eve", 20},
	}

	// 이름으로 정렬
	sort.SliceStable(people, func(i, j int) bool {
		return people[i].Name < people[j].Name
	})
	fmt.Println("이름으로 정렬 sort.SliceStable:")
	for _, p := range people {
		fmt.Printf("%s - Age: %d\n", p.Name, p.Age)
	}

	fmt.Printf("\n")

	// 나이로 정렬
	sort.SliceStable(people, func(i, j int) bool {
		return people[i].Age < people[j].Age
	})
	fmt.Println("나이로 정렬 sort.SliceStable:")
	for _, p := range people {
		fmt.Printf("%s - Age: %d\n", p.Name, p.Age)
	}
}

결과값:

이름으로 정렬 sort.SliceStable:
Alice - Age: 30              
Alice - Age: 10              
Alice - Age: 20              
Bob - Age: 30                
Bob - Age: 10                
Bob - Age: 20                
Charlie - Age: 30            
Charlie - Age: 10            
Charlie - Age: 20            
David - Age: 30              
David - Age: 10              
David - Age: 20              
Eve - Age: 30                
Eve - Age: 10                
Eve - Age: 20                
                             
나이로 정렬 sort.SliceStable:
Alice - Age: 10              
Bob - Age: 10                
Charlie - Age: 10            
David - Age: 10              
Eve - Age: 10                
Alice - Age: 20              
Bob - Age: 20                
Charlie - Age: 20
David - Age: 20
Eve - Age: 20
Alice - Age: 30
Bob - Age: 30
Charlie - Age: 30
David - Age: 30
Eve - Age: 30

원래의 순서가 보장되는것을 알수있다

 

 

참고:

https://pkg.go.dev/sort

 

sort package - sort - Go Packages

ExampleMultiKeys demonstrates a technique for sorting a struct type using different sets of multiple fields in the comparison. We chain together "Less" functions, each of which compares a single field. package main import ( "fmt" "sort" ) // A Change is a

pkg.go.dev

https://cs.opensource.google/go/go/+/refs/tags/go1.20.6:src/sort/slice.go 

 

https://cs.opensource.google/go/go/+/refs/tags/go1.20.6:src/sort/slice.go

 

cs.opensource.google

https://go.dev/src/sort/zsortfunc.go

 

- The Go Programming Language

 

go.dev