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?

【PowerShell】Sort-Object で配列の並び替え

Posted at

はじめに

Azure CosmosDB の Rest API を利用してデータを取得する際に ORDER BY など一部のクエリが利用できないようでした。
そこで データ取得後に PowerShell の処理で並べ替えを実装することにしました。

環境

  • macOS: 15.0.1
  • PowerShell: 7.4.0

Sort-Object とは?

プロパティの値などを利用して、配列などのオブジェクトを並べ替えができる PowerShell のメソッドです。
(PowerShell はメソッドという表現でいいのだろうか?)

使い方

Sort-Object -Property <プロパティ> -Descending

オプションは色々とあるみたいですが、今回は -Property と -Descending のみ指定して実行します。
これで プロパティの値に基づいて、降順に並び替えることができます。

実際に使ってみる

$sampleData = @(
  {
    id: 25,
    name: "Pikachu"
  },
  {
    id: 151,
    name: "mewtwo"
  },
  {
    id: 6,
    name: "Charizard"
  }
)

$sampleData | Sort-Object -Property id -Descending

# 実行結果:
#
# id: 151,
#   name: "mewtwo"
#
#
#   id: 25,
#   name: "Pikachu"
#
#
#   id: 6,
#   name: "Charizard"

ちゃんと並び替えできました。

さいごに

今回は、PowerShell で Sort-Object の配列の並び替えを記事にしてみました。
簡単な内容でも忘れそうなので、こういったものも記事にして記憶に残そうと思います。

最後まで見ていただきありがとうございました。
誰かの役に立てば幸いです。

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?