###前提・実現したいこと
Java8のStreamを使用して自作オブジェクトのListの並べ替えをしたいと考えています
テストとして、PersonクラスとSectionクラス、その二つを持っているInfoクラスを作りました
- Personクラス
java
1package sorttest; 2 3public class Person { 4 5 String firstName; 6 String lastName; 7 int age; 8 9 public Person(String firstName, String lastName, int age) { 10 this.firstName = firstName; 11 this.lastName = lastName; 12 this.age = age; 13 } 14 15 public String getFirstName() { 16 return firstName; 17 } 18 19 public String getLastName() { 20 return lastName; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 @Override 28 public String toString() { 29 return "Person{" + "firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + '}'; 30 } 31}
- Sectionクラス
java
1package sorttest; 2 3public class Section { 4 5 String sectionCode; 6 String sectionName; 7 8 public Section(String sectionCode, String sectionName) { 9 this.sectionCode = sectionCode; 10 this.sectionName = sectionName; 11 } 12 13 public String getSectionCode() { 14 return sectionCode; 15 } 16 17 public String getSectionName() { 18 return sectionName; 19 } 20 21 @Override 22 public String toString() { 23 return "Section{" + "sectionCode=" + sectionCode + ", sectionName=" + sectionName + '}'; 24 } 25}
- Infoクラス
java
1package sorttest; 2 3public class Info { 4 5 Person person; 6 Section section; 7 8 public Info(Person person, Section section) { 9 this.person = person; 10 this.section = section; 11 } 12 13 public Person getPerson() { 14 return person; 15 } 16 17 public Section getSection() { 18 return section; 19 } 20 21 @Override 22 public String toString() { 23 return "Info{" + "person=" + person.toString() + ", section=" + section.toString() + '}'; 24 } 25}
###実行したソースコード
以下の様に単純な形のリストだとStreamでcomparingを使用して並び替えができます
java
1package sorttest; 2 3import java.util.ArrayList; 4import java.util.Comparator; 5import java.util.List; 6import java.util.stream.Collectors; 7 8public class SortTest { 9 10 public static void main(String[] args) { 11 12 List<Section> sectionList = new ArrayList<>(); 13 14 sectionList.add(new Section("A", "あああ")); 15 sectionList.add(new Section("C", "ううう")); 16 sectionList.add(new Section("D", "えええ")); 17 sectionList.add(new Section("B", "ううう")); 18 19 20 System.out.println("sectionListをループ"); 21 for (Section section : sectionList) { 22 System.out.println(section.toString()); 23 } 24 25 System.out.println("-------------------------------------------------------"); 26 System.out.println("-------------------------------------------------------"); 27 System.out.println("-------------------------------------------------------"); 28 29 System.out.println("sectionListをsectionNameでソートしてからループ"); 30 31 List<Section> sectionList2 = new ArrayList<>(); 32 sectionList2.addAll(sectionList.stream().sorted( 33 Comparator.comparing(Section::getSectionCode)) 34 .collect(Collectors.toList())); 35 for (Section section : sectionList2) { 36 System.out.println(section.toString()); 37 } 38}
- 実行結果
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
1package sorttest; 2 3import java.util.ArrayList; 4import java.util.Comparator; 5import java.util.List; 6import java.util.stream.Collectors; 7 8public class SortTest { 9 10 public static void main(String[] args) { 11 System.out.println("infoListをループ"); 12 List<Info> infoList = new ArrayList<>(); 13 infoList.add(new Info(new Person("taro", "yamada", 30), new Section("A", "あああ"))); 14 infoList.add(new Info(new Person("sachiko", "sato", 28), new Section("C", "ううう"))); 15 infoList.add(new Info(new Person("ichiro", "suzuki", 40), new Section("D", "えええ"))); 16 infoList.add(new Info(new Person("Eri", "ito", 20), new Section("B", "ううう"))); 17 18 for (Info info : infoList) { 19 System.out.println(info.getPerson().toString()); 20 System.out.println(info.getSection().toString()); 21 System.out.println("-------------------------------------------------------"); 22 } 23 } 24}
- 実行結果
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=えええ} -------------------------------------------------------
- よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。