0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

配列の中身を小さい順に並び替えて出力してみた。[初心者向け]

Last updated at Posted at 2021-04-04

こんにちは!!

今回は配列の中身を小さい順に並べてみました!!!

問題

l := []int{100, 300, 23, 11, 23, 2, 4, 6, 4}
中身を小さい順に並べます。

こんな感じ。。。

image.png

回答

自分の回答こちら!!!

package main

import (
	"fmt"
	"sort"
)

func main() {
	l := []int{100, 300, 23, 11, 23, 2, 4, 6, 4}
	sort.Sort(sort.IntSlice(l))
	fmt.Println(l[0:9])
}

sortのインターフェースというのがあるらしく、
勝手に小さい順にやってくれました。笑
理解していないからまだまだですな。。。

参考になるサイト
https://qiita.com/Jxck_/items/fb829b818aac5b5f54f7

いろんな書き方あるから引き出し(いろんなパターンのコードの書き方)を増やします。。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?