###前提・実現したいこと
Java8のStreamを使用して自作オブジェクトのListの並べ替えをしたいと考えています
テストとして、PersonクラスとSectionクラス、その二つを持っているInfoクラスを作りました
- Personクラス
java
package sorttest; public class Person { String firstName; String lastName; int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } @Override public String toString() { return "Person{" + "firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + '}'; } }
- Sectionクラス
java
package sorttest; public class Section { String sectionCode; String sectionName; public Section(String sectionCode, String sectionName) { this.sectionCode = sectionCode; this.sectionName = sectionName; } public String getSectionCode() { return sectionCode; } public String getSectionName() { return sectionName; } @Override public String toString() { return "Section{" + "sectionCode=" + sectionCode + ", sectionName=" + sectionName + '}'; } }
- Infoクラス
java
package sorttest; public class Info { Person person; Section section; public Info(Person person, Section section) { this.person = person; this.section = section; } public Person getPerson() { return person; } public Section getSection() { return section; } @Override public String toString() { return "Info{" + "person=" + person.toString() + ", section=" + section.toString() + '}'; } }
###実行したソースコード
以下の様に単純な形のリストだとStreamでcomparingを使用して並び替えができます
java
package sorttest; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class SortTest { public static void main(String[] args) { List<Section> sectionList = new ArrayList<>(); sectionList.add(new Section("A", "あああ")); sectionList.add(new Section("C", "ううう")); sectionList.add(new Section("D", "えええ")); sectionList.add(new Section("B", "ううう")); System.out.println("sectionListをループ"); for (Section section : sectionList) { System.out.println(section.toString()); } System.out.println("-------------------------------------------------------"); System.out.println("-------------------------------------------------------"); System.out.println("-------------------------------------------------------"); System.out.println("sectionListをsectionNameでソートしてからループ"); List<Section> sectionList2 = new ArrayList<>(); sectionList2.addAll(sectionList.stream().sorted( Comparator.comparing(Section::getSectionCode)) .collect(Collectors.toList())); for (Section section : sectionList2) { System.out.println(section.toString()); } }
- 実行結果
sectionListをループ Section{sectionCode=A, sectionName=あああ} Section{sectionCode=C, sectionName=ううう} Section{sectionCode=D, sectionName=えええ} Section{sectionCode=B, sectionName=ううう} ------------------------------------------------------- ------------------------------------------------------- ------------------------------------------------------- sectionListをsectionNameでソートしてからループ Section{sectionCode=A, sectionName=あああ} Section{sectionCode=B, sectionName=ううう} Section{sectionCode=C, sectionName=ううう} Section{sectionCode=D, sectionName=えええ}
###はまっているところ
以下のようにInfoオブジェクトの中にあるSectionオブジェクトの複数フィールドでソートしたい場合はどうするのがいいのでしょうか?
java
package sorttest; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class SortTest { public static void main(String[] args) { System.out.println("infoListをループ"); List<Info> infoList = new ArrayList<>(); infoList.add(new Info(new Person("taro", "yamada", 30), new Section("A", "あああ"))); infoList.add(new Info(new Person("sachiko", "sato", 28), new Section("C", "ううう"))); infoList.add(new Info(new Person("ichiro", "suzuki", 40), new Section("D", "えええ"))); infoList.add(new Info(new Person("Eri", "ito", 20), new Section("B", "ううう"))); for (Info info : infoList) { System.out.println(info.getPerson().toString()); System.out.println(info.getSection().toString()); System.out.println("-------------------------------------------------------"); } } }
- 実行結果
infoListをループ Person{firstName=taro, lastName=yamada, age=30} Section{sectionCode=A, sectionName=あああ} ------------------------------------------------------- Person{firstName=sachiko, lastName=sato, age=28} Section{sectionCode=C, sectionName=ううう} ------------------------------------------------------- Person{firstName=ichiro, lastName=suzuki, age=40} Section{sectionCode=D, sectionName=えええ} ------------------------------------------------------- Person{firstName=Eri, lastName=ito, age=20} Section{sectionCode=B, sectionName=ううう} -------------------------------------------------------
- SectionのsectionNameでソートして以下のようにしたい
- **追記・・・**sectionNameだけでなくsectionCodeと合わせてソートするにはどうすればいいでしょうか?
Person{firstName=taro, lastName=yamada, age=30} Section{sectionCode=A, sectionName=あああ} ------------------------------------------------------- Person{firstName=Eri, lastName=ito, age=20} Section{sectionCode=B, sectionName=ううう} ------------------------------------------------------- Person{firstName=sachiko, lastName=sato, age=28} Section{sectionCode=C, sectionName=ううう} ------------------------------------------------------- Person{firstName=ichiro, lastName=suzuki, age=40} Section{sectionCode=D, sectionName=えええ} -------------------------------------------------------
- よろしくお願いします。
まだ回答がついていません
会員登録して回答してみよう