次の配列をソートする必要があります。
String [][] myArray = {{"John", "3", "10"}, {"Paul", "16","2"},
{"Mike","8","20"}, {"Peter","5","0"}};
次の配列に:
String [][] myArray = {{"John", "3", "10"}, {"Mike","8","20"},
{"Paul", "16","2"}, {"Peter","5","0"}};
私は2つのクラスを持っています。最初のものには配列が含まれ、もう 1 つは Comparator インターフェイスを実装します。これは MyArray クラスです。
public class MyArray {
public static void main(String[] args) {
String [][] myArray = {{"John", "3", "10"}, {"Paul", "16","2"},
{"Mike","8","20"},{"Peter","5","0"}};
ByName byName = new ByName();
Arrays.sort(myArray,byName);
}
}
これは ByName クラスです。
import java.util.Comparator;
public class ByName implements Comparator {
public int compare(Object o1, Object o2) {
String name1 = (String)((MyArray) o1)[0]);
String name2 = (String)((MyArray) o2)[0]);
return (name1>name2) ? 1 : -1;
// Here I am confused, how can I compare it, how can I sort my array
// alphabetically by the first element of each element, i.e. by the names.
}
}