paiza.ioでelixir その277

# 概要
paiza.ioでelixirやってみた。
sortやってみた。

# サンプルコード

```



defmodule QuickSort do
	def qsort([pivot | t]) do
		qsort(for x <- t, x < pivot, do: x)
		++ [pivot] ++
		qsort(for x <- t, x >= pivot, do: x)
	end
	def qsort([]), do: []
end

[7, 2, 3, 1, 4, 5, 6] 
|>QuickSort.qsort
|>IO.inspect 


for n <- 1..10, rem(n, 2) == 0, do: n * n
|>IO.inspect 


[7, 2, 3, 1, 4, 5, 6] 
|> Enum.sort()
|>IO.inspect 

[7, 2, 3, 1, 4, 5, 6] 
|> Enum.sort(:desc)
|>IO.inspect 


words = ["some", "kind", "of", "monster"]

Enum.sort words, &(String.length(&1) < String.length(&2))
|>IO.inspect 

Enum.sort words, &(String.length(&1) <= String.length(&2))
|>IO.inspect 



"cabed" 
|> String.codepoints() 
|> Enum.sort()
|>IO.inspect 

"cabed" 
|> String.codepoints() 
|> Enum.sort(:desc)
|>IO.inspect 


defmodule Player do
	defstruct player_id: 0, score: 0
end

defmodule Sort do
	def run do
		[{1, 50}, {2, 20}, {3, 100}]
		|> Enum.map(fn {player_id, score} ->
			%Player{player_id: player_id, score: score}
		end)
		|> Enum.sort_by(fn %{score: score} -> 
			score 
		end)
	end
end
Sort.run
|>IO.inspect 

defmodule Sort do
	def run do
		[{1, 50}, {2, 20}, {3, 100}]
		|> Enum.map(fn {player_id, score} ->
			%Player{player_id: player_id, score: score}
		end)
		|> Enum.sort_by(fn %{score: score} -> 
			score 
		end, :desc)
	end
end
Sort.run
|>IO.inspect 


words = ["にほん", "あめりか", "ろしあ", "ふらんす", "いたりあ", "ちゅうごく", "しんがぽーる", "おーすとらりあ", "たい", "ちり", "どいつ", "いぎりす"]

Enum.sort_by(words, fn word -> 
    {String.length(word), word} 
end)  
|>IO.inspect 

Enum.sort_by(words, &({String.length(&1), &1}))                
|>IO.inspect 


Enum.sort_by([2, 3, 1], &(&1), :asc)
|>IO.inspect 


Enum.sort_by([2, 3, 1], &(&1), :desc)
|>IO.inspect 

users = [
   %{name: "Ellis", birthday: ~D[1943-05-11]},
   %{name: "Lovelace", birthday: ~D[1815-12-10]},
   %{name: "Turing", birthday: ~D[1912-06-23]}
]


Enum.sort_by(users, fn x -> 
    x.birthday
end, {:desc, Date})  
|>IO.inspect 

Enum.sort_by(users, &(&1.birthday), Date)
|>IO.inspect 

Enum.sort_by(users, &(&1.birthday), {:desc, Date})
|>IO.inspect 



[
  %{key1: 100, key2: 200, name: "map1"},
  %{key1: 99, key2: 201, name: "map2"},
  %{key1: 101, key2: 199, name: "map3"},
  %{key1: 100, key2: 201, name: "map4"}
]


|>Enum.sort(fn(x, y) -> 
    x.key1 < y.key1  
end)
|>IO.inspect 


|>Enum.sort(fn(x, y) -> 
    x.key1 <= y.key1  
end)
|>IO.inspect 


[
  %{
	id: 1,
	name: "ryu",
	age: 10,
	gaku: "s",
	inserted_at: ~N[2023-10-01 05:35:23],
	updated_at: ~N[2023-11-30 05:35:37]
  },
  %{
	id: 2,
	name: "ryo",
	age: 20,
	gaku: "s",
	inserted_at: ~N[2023-10-03 05:35:23],
	updated_at: ~N[2023-11-30 05:35:37]
  },
  %{
	id: 3,
	name: "alice",
	age: 20,
	gaku: "s",
	inserted_at: ~N[2023-10-05 05:35:23],
	updated_at: ~N[2023-11-30 05:35:37]
  },
  %{
	id: 4,
	name: "kent",
	age: 30,
	gaku: "s",
	inserted_at: ~N[2023-09-05 05:35:23],
	updated_at: ~N[2023-11-30 05:35:37]
  }
]

|>Enum.sort_by(fn x -> 
    x.inserted_at
end, {:desc, NaiveDateTime})  

|>IO.inspect 



```

# 実行結果

```
[1, 2, 3, 4, 5, 6, 7]
4
16
36
64
100
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]
false
false
true
true
true
true
true
true
false
false
true
["a", "b", "c", "d", "e"]
["e", "d", "c", "b", "a"]
[
  %Player{player_id: 2, score: 20},
  %Player{player_id: 1, score: 50},
  %Player{player_id: 3, score: 100}
]
[
  %Player{player_id: 3, score: 100},
  %Player{player_id: 1, score: 50},
  %Player{player_id: 2, score: 20}
]
["たい", "ちり", "どいつ", "にほん", "ろしあ", "あめりか",
 "いぎりす", "いたりあ", "ふらんす", "ちゅうごく",
 "しんがぽーる", "おーすとらりあ"]
["たい", "ちり", "どいつ", "にほん", "ろしあ", "あめりか",
 "いぎりす", "いたりあ", "ふらんす", "ちゅうごく",
 "しんがぽーる", "おーすとらりあ"]
[1, 2, 3]
[3, 2, 1]
[
  %{birthday: ~D[1943-05-11], name: "Ellis"},
  %{birthday: ~D[1912-06-23], name: "Turing"},
  %{birthday: ~D[1815-12-10], name: "Lovelace"}
]
[
  %{birthday: ~D[1815-12-10], name: "Lovelace"},
  %{birthday: ~D[1912-06-23], name: "Turing"},
  %{birthday: ~D[1943-05-11], name: "Ellis"}
]
[
  %{birthday: ~D[1943-05-11], name: "Ellis"},
  %{birthday: ~D[1912-06-23], name: "Turing"},
  %{birthday: ~D[1815-12-10], name: "Lovelace"}
]
[
  %{key1: 99, key2: 201, name: "map2"},
  %{key1: 100, key2: 201, name: "map4"},
  %{key1: 100, key2: 200, name: "map1"},
  %{key1: 101, key2: 199, name: "map3"}
]
[
  %{key1: 99, key2: 201, name: "map2"},
  %{key1: 100, key2: 201, name: "map4"},
  %{key1: 100, key2: 200, name: "map1"},
  %{key1: 101, key2: 199, name: "map3"}
]
[
  %{
    age: 20,
    gaku: "s",
    id: 3,
    inserted_at: ~N[2023-10-05 05:35:23],
    name: "alice",
    updated_at: ~N[2023-11-30 05:35:37]
  },
  %{
    age: 20,
    gaku: "s",
    id: 2,
    inserted_at: ~N[2023-10-03 05:35:23],
    name: "ryo",
    updated_at: ~N[2023-11-30 05:35:37]
  },
  %{
    age: 10,
    gaku: "s",
    id: 1,
    inserted_at: ~N[2023-10-01 05:35:23],
    name: "ryu",
    updated_at: ~N[2023-11-30 05:35:37]
  },
  %{
    age: 30,
    gaku: "s",
    id: 4,
    inserted_at: ~N[2023-09-05 05:35:23],
    name: "kent",
    updated_at: ~N[2023-11-30 05:35:37]
  }
]


```

# 成果物

https://paiza.io/projects/SbPRuV97w5MxyY7NXF3iZg?language=elixir


以上。