LoginSignup
1
0

More than 1 year has passed since last update.

golangのnilとか入った構造体のsortこんな感じでOKそう?

Last updated at Posted at 2022-03-30

はじめに

結果みる感じ問題なさそうですが、
不安だったので、
不安そうなタイトルにしました!:chicken:

sortのおさらいしたので問題なさそうになりました!ヽ(´▽`)/
(ただ不可思議なこともあるのでそこは気になっている所存です、、赤く書いてます。)

もっとシンプル綺麗に書けるよ!
という優しい方がいたら教えて頂けると嬉しいです!:bow_tone1:

@Nabetani さんがコメントしてくださいました! ありがとうございます!:bow_tone1:

【実際のリンク】
https://qiita.com/Nabetani/items/62f1a1d730d4e26be6e4

@Nabetani さんの方が不要なsortがないかつスッキリしているのでコード書くときは、
@Nabetani さんの参考にするといいかと思います!
(コールバック関数で必要なプロパティを返すのや、0で次の処理、-1と1で変換するしないを使い分けたり勉強になることがいっぱいでした!!(>ω<) 本当にありがとうございます!)

お試し等は以下にペタッと貼り付けたりして動かすといいのではないかと思ってます!
https://go.dev/play/

主な参考

nilがある場合のsort方法はこうやるんだよの参考。
https://stackoverflow.com/questions/48331747/ignore-nil-elements-when-sorting

sortした内容を次のsortで引き継ぎたい時『SliceStable』を使うとできるよの参考
https://qiita.com/Sekky0905/items/2d5ccd6d076106e9d21c

以下実行した場合の最初と最後の出力結果比較

.txt
何もしない場合の出力確認
<nil> <nil> <nil>
名前01 説明2 <nil>
名前02 <nil> メモ2
名前01 説明1 <nil>
<nil> 説明1 <nil>
<nil> <nil> <nil>
名前02 <nil> メモ1
↓
NameとNoteとDiscriptionsort場合の出力確認(3回目)
名前01 説明1 <nil>
名前01 説明2 <nil>
名前02 <nil> メモ1
名前02 <nil> メモ2
<nil> 説明1 <nil>
<nil> <nil> <nil>
<nil> <nil> <nil>

sortについて少し復習

注意: goの中のソース解読したわけではないので違い等あるかもしれません
sortの中は何回か最初から最後までの並び替えを行なっているようです!以下参考
https://products.sint.co.jp/topsic/blog/algorithm-type

そして今回きもとなる箇所のソートのおさらい。
ほぼ図を見てもらえればわかってくださるのではないかと思ってます!

スクリーンショット 2022-04-03 19.13.01.png
※赤い箇所ですが2回目があるとうまくいかない(falseでないと)という感じになります。
スクリーンショット 2022-04-03 17.52.54.png
スクリーンショット 2022-04-03 17.47.47.png

code

.go
package main

import (
	"fmt"
	"sort"
)

type Sample struct {
	Name        *string
	Discription *string
	Note        *string
}

func change(s string) *string {
	return &s
}

func changeString(s *string) interface{} {
	if s == nil {
		return nil
	} else {
		return *s
	}
}

func main() {
	sample_01 := &Sample{Name: nil, Discription: nil, Note: nil}
	sample_02 := &Sample{Name: change("名前01"), Discription: change("説明2"), Note: nil}
	sample_03 := &Sample{Name: change("名前02"), Discription: nil, Note: change("メモ2")}
	sample_04 := &Sample{Name: change("名前01"), Discription: change("説明1"), Note: nil}
	sample_05 := &Sample{Name: nil, Discription: change("説明1"), Note: nil}
	sample_06 := &Sample{Name: nil, Discription: nil, Note: nil}
	sample_07 := &Sample{Name: change("名前02"), Discription: nil, Note: change("メモ1")}

	sample_array := [...]*Sample{
		sample_01,
		sample_02,
		sample_03,
		sample_04,
		sample_05,
		sample_06,
		sample_07,
	}

	fmt.Println("\n" + "何もしない場合の出力確認")
	for _, v := range sample_array {
		fmt.Println(changeString(v.Name), changeString(v.Discription), changeString(v.Note))
	}

	//Noteのsort
	sort.SliceStable(sample_array[:], func(i, j int) bool {
		if sample_array[i].Note != nil && sample_array[j].Note != nil {
			return string(*(sample_array[i].Note)) < string(*(sample_array[j].Note))
		}
		if sample_array[i].Note != nil && sample_array[j].Note == nil {
			return true
		}
		return false
	})

	fmt.Println("\n" + "Noteのsort場合の出力確認(1回目)")
	for _, v := range sample_array {
		fmt.Println(changeString(v.Name), changeString(v.Discription), changeString(v.Note))
	}

	//Discriptionのsort Noteのsortを引き継いでいます。
	sort.SliceStable(sample_array[:], func(i, j int) bool {
		if sample_array[i].Discription != nil && sample_array[j].Discription != nil {
			return string(*(sample_array[i].Discription)) < string(*(sample_array[j].Discription))
		}
		if sample_array[i].Discription != nil && sample_array[j].Discription == nil {
			return true
		}
		return false
	})

	fmt.Println("\n" + "NoteとDiscriptionのsort場合の出力確認(2回目)")
	for _, v := range sample_array {
		fmt.Println(changeString(v.Name), changeString(v.Discription), changeString(v.Note))
	}

	//Nameのsort Discriptionのsort、Noteのsortを引き継いでいます。
	sort.SliceStable(sample_array[:], func(i, j int) bool {
		if sample_array[i].Name != nil && sample_array[j].Name != nil {
			return string(*(sample_array[i].Name)) < string(*(sample_array[j].Name))
		}
		if sample_array[i].Name != nil && sample_array[j].Name == nil {
			return true
		}
		return false
	})

	fmt.Println("\n" + "NameとNoteとDiscriptionsort場合の出力確認(3回目)")
	for _, v := range sample_array {
		fmt.Println(changeString(v.Name), changeString(v.Discription), changeString(v.Note))
	}

}


最後に

sort難しいと思うの私だけでしょうか?:sweat_smile:
あと他言語パターンとかあったらいいなと思いました!:shamrock:

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0