質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

コンストラクタ

オブジェクト指向言語において、オブジェクトを生成時に呼び出され、データの初期化などを行なう関数・メソッドのことである。

ソート

複数のデータを、順序性に従って並べ替えること。 データ処理を行う際に頻繁に用いられ、多くのアルゴリズムが存在します。速度、容量、複雑さなどに違いがあり、高速性に特化したものにクイックソートがあります。

Q&A

解決済

1回答

629閲覧

値の受け渡し後、並び替えを行いたいですがうまく実行されません

channi

総合スコア6

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

コンストラクタ

オブジェクト指向言語において、オブジェクトを生成時に呼び出され、データの初期化などを行なう関数・メソッドのことである。

ソート

複数のデータを、順序性に従って並べ替えること。 データ処理を行う際に頻繁に用いられ、多くのアルゴリズムが存在します。速度、容量、複雑さなどに違いがあり、高速性に特化したものにクイックソートがあります。

0グッド

0クリップ

投稿2019/11/16 06:34

編集2019/11/16 06:39

星占いの作成を行っております。
並び替えを行いたいと考え、コンストラクタを使っております。

new Constellationの値の二つを受け取り
その下で並び替えを行うように使っておりますが
値がうまく受け取れていないいようで並び替えがうまく行われません。

どこを変更すればよいでしょうか。

C#

1 private Constellation[] ConstellationList = 2 { 3 new Constellation("07", "牡羊座"), 4 new Constellation("78", "牡牛座"), 5 new Constellation("38", "双子座"), 6 new Constellation("12", "蟹座"), 7 new Constellation("46", "獅子座"), 8 new Constellation("86", "乙女座"), 9 new Constellation("49", "天秤座"), 10 new Constellation("73", "蠍座"), 11 new Constellation("72", "射手座"), 12 new Constellation("16", "山羊座"), 13 new Constellation("04", "水瓶座"), 14 new Constellation("66", "魚座") 15 }; 16 private class Constellation : IComparable<Constellation> 17 { 18 private string hoge { get; set; } 19 20 21 22 } 23 24 25 26 private void Button2_Click(object sender, EventArgs e) 27 { 28 //日付ボックスから日付をyyyyMMdd フォーマットで取得する 29 string dateStr = dateTimePicker1.Value.ToString("yyyyMMdd"); 30 31 //文字列を数値に変換する 32 int dateInt = Convert.ToInt32(dateStr); 33 34 // 再計算する 35 foreach (Constellation constellation in ConstellationList) 36 { 37 constellation.calcResult(dateInt); 38 } 39 40 // 並び替え 41 Array.Sort(ConstellationList); 42 43 // 表示し直す 44 dispResult(dCodeLabel1, dConstellationLabel1, dResultLabel1, ConstellationList[0]); //一行目 45 dispResult(dCodeLabel2, dConstellationLabel2, dResultLabel2, ConstellationList[1]); //二行目 46 dispResult(dCodeLabel3, dConstellationLabel3, dResultLabel3, ConstellationList[2]); //三行目 47 dispResult(dCodeLabel4, dConstellationLabel4, dResultLabel4, ConstellationList[3]); //四行目 48 dispResult(dCodeLabel5, dConstellationLabel5, eResultLabel5, ConstellationList[4]); //五行目 49 dispResult(dCodeLabel6, dConstellationLabel6, dResultLabel6, ConstellationList[5]); //六行目 50 dispResult(dCodeLabel7, dConstellationLabel7, dResultLabel7, ConstellationList[6]); //七行目 51 dispResult(dCodeLabel8, dConstellationLabel8, dResultLabel8, ConstellationList[7]); //八行目 52 dispResult(dCodeLabel9, dConstellationLabel9, dResultLabel9, ConstellationList[8]); // 九行目 53 dispResult(dCodeLabel10, dConstellationLabel10, dResultLabel10, ConstellationList[9]); //十行目 54 dispResult(dCodeLabel11, dConstellationLabel11, dResultLabel11, ConstellationList[10]); //十一行目 55 dispResult(dCodeLabel12, dConstellationLabel12, dResultLabel12, ConstellationList[11]); //十二行目 56 } 57 58 59 // 各行に再表示するメソッド 60 private void dispResult(Label codeLabel, Label constellationLabel, Label resultLabel, Constellation constellation) 61 { 62 codeLabel.Text = constellation.Code; 63 constellationLabel.Text = constellation.Name; 64 resultLabel.Text = constellation.ResultStr; 65

使用しいるフレームワークは、
.NET Framewok4.7.2 です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hihijiji

2019/11/16 06:43

Constellation クラスをキチンと書いてください。
hihijiji

2019/11/16 06:45

Constellation クラスのコンストラクタの書き方がわからないってことでしたら 失礼しました。
channi

2019/11/16 06:47

すみません。 そこがいまいちまだ理解できていません。。。
guest

回答1

0

ベストアンサー

こんなクラスが欲しいのかな?

C#

1private class Constellation : IComparable<Constellation> 2{ 3 public string Code { get; } 4 5 public string Name { get; } 6 7 public Constellation(string code, string name) 8 { 9 Code = code; 10 Name = name; 11 } 12 13 public int CompareTo(Constellation other) 14 { 15 return this.Code.CompareTo(other.Code); 16 } 17}

投稿2019/11/16 06:55

hihijiji

総合スコア4150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

channi

2019/11/16 07:00

なるほど!!! いろいろ解説見てましたが、あまりピンときてませんでした。 一度、試してみます。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問