複数の文字列を格納したArrayListのソート


このリストをs1で文字列でソートしたいのですが標準のソートを適用するのは難しい  
しかし、一から文字列のソートを書くのはコードが大変そうです  
s1とs2は紐づけしたまま扱いたい何かいい案はありませんか？

[code]

    public class MultiList {
        public class String2{
            private String s1;
            private String s2;
    
            public void setString1(String text1) {
                s1 = text1;
            }
            public void setString2(String text2) {
                s2 = text2;
            }
    
            public String getString1() {
                return s1;
            }
            public String getString2() {
                return s2;
            }
        }
    
    
    
        public ArrayList<String2> String2List(ArrayList<String> s1, ArrayList<String> s2){
            //二つのリストからオブジェクト型のリストを返す
            ArrayList<String2> String2List = new ArrayList<String2>();
            if(s1.size() == s2.size()){
                for(int i = 0; i<s1.size();i++){
                    String2 String2 = new String2();
                    String2.setString1(s1.get(i));
                    String2.setString2(s2.get(i));
                }
                return String2List;
            }
            return String2List;
        }
    }
    
[/code]




標準のソートじゃないなら、[`java.util.Collections.sort(list,
comparator)`](https://docs.oracle.com/javase/jp/7/api/java/util/Collections.html#sort\(java.util.List,%20java.util.Comparator\))使ってみて。例:

[code]

    ArrayList<String2> merged = String2List(list_1, list_2);
    java.util.Collections.sort(merged, new java.util.Comparator<String2>(){
            public int compare(String2 a, String2 b) {
            return a.getString1().compareTo(b.getString1());
        }
    });
    
[/code]

完全な例:

[code]

    import java.util.ArrayList;
    
    public class MultiList {
        public static void main(String args[]){
            ArrayList<String> list_1 = new ArrayList<String>();
            java.util.Collections.addAll(list_1, "c", "b", "a");
            ArrayList<String> list_2 = new ArrayList<String>();
            java.util.Collections.addAll(list_2, "Tom", "Peter", "Harry");
    
            ArrayList<String2> merged = String2List(list_1, list_2);
            java.util.Collections.sort(merged, new java.util.Comparator<String2>(){
                  public int compare(String2 a, String2 b) {
                     return a.getString1().compareTo(b.getString1());
                }
            });
    
            for(String2 s:merged){
                System.out.println(s.getString1() + ", " + s.getString2());
            }
        }
    
        public static ArrayList<String2> String2List(ArrayList<String> s1, ArrayList<String> s2){
            ArrayList<String2> String2List = new ArrayList<String2>();
            if(s1.size() == s2.size()){
                for(int i = 0; i<s1.size();i++){
                    String2 String2 = new String2();
                    String2.setString1(s1.get(i));
                    String2.setString2(s2.get(i));
                    String2List.add(String2); // 注意
                }
            }
            return String2List;
        }
    
        public static class String2{
            private String s1;
            private String s2;
    
            public void setString1(String text1) {
                s1 = text1;
            }
            public void setString2(String text2) {
                s2 = text2;
            }
    
            public String getString1() {
                return s1;
            }
            public String getString2() {
                return s2;
            }
        }
    }
    
[/code]

その出力:

[code]

    a, Harry
    b, Peter
    c, Tom
    
[/code]




標準のソートじゃないなら、[`java.util.Collections.sort(list,
comparator)`](https://docs.oracle.com/javase/jp/7/api/java/util/Collections.html#sort\(java.util.List,%20java.util.Comparator\))使ってみて。例:

[code]

    ArrayList<String2> merged = String2List(list_1, list_2);
    java.util.Collections.sort(merged, new java.util.Comparator<String2>(){
            public int compare(String2 a, String2 b) {
            return a.getString1().compareTo(b.getString1());
        }
    });
    
[/code]

完全な例:

[code]

    import java.util.ArrayList;
    
    public class MultiList {
        public static void main(String args[]){
            ArrayList<String> list_1 = new ArrayList<String>();
            java.util.Collections.addAll(list_1, "c", "b", "a");
            ArrayList<String> list_2 = new ArrayList<String>();
            java.util.Collections.addAll(list_2, "Tom", "Peter", "Harry");
    
            ArrayList<String2> merged = String2List(list_1, list_2);
            java.util.Collections.sort(merged, new java.util.Comparator<String2>(){
                  public int compare(String2 a, String2 b) {
                     return a.getString1().compareTo(b.getString1());
                }
            });
    
            for(String2 s:merged){
                System.out.println(s.getString1() + ", " + s.getString2());
            }
        }
    
        public static ArrayList<String2> String2List(ArrayList<String> s1, ArrayList<String> s2){
            ArrayList<String2> String2List = new ArrayList<String2>();
            if(s1.size() == s2.size()){
                for(int i = 0; i<s1.size();i++){
                    String2 String2 = new String2();
                    String2.setString1(s1.get(i));
                    String2.setString2(s2.get(i));
                    String2List.add(String2); // 注意
                }
            }
            return String2List;
        }
    
        public static class String2{
            private String s1;
            private String s2;
    
            public void setString1(String text1) {
                s1 = text1;
            }
            public void setString2(String text2) {
                s2 = text2;
            }
    
            public String getString1() {
                return s1;
            }
            public String getString2() {
                return s2;
            }
        }
    }
    
[/code]

その出力:

[code]

    a, Harry
    b, Peter
    c, Tom
    
[/code]

