Java sort 2D String Array by third column number
I am given a text file with countries, names and dozens of skaters. I have to read each line in an array and then sort the array in descending order.
I managed to organize everything like this:
infoArray[0][0] = "GER";
infoArray[0][1] = "Nathalie WEINZIERL Maylin & Daniel Peter LIEBERS Nelli & Alexander";
infoArray[0][2] = "17.0";
infoArray[1][0] = "RUS";
infoArray[1][1] = "Evgeny PLYUSHCHENKO Tatiana & Maxim Yulia LIPNITSKAYA Ekaterina & Dmitri"
infoArray[1][2] = "37.0";
infoArray[2][0] = "CHN";
infoArray[2][1] = "Kexin ZHANG Han YAN Cheng & Hao Xintong & Xun"
infoArray[2][2] = "20.0"
And so on for 10 different countries.
Now I need to display 10 different countries in descending order using the third column, which is a numeric value (although it is in string format).
So, in other words, I want the output of these three countries to be as follows:
RUS: 37.0
CHN: 20.0
GER: 17.0
I was thinking about using nested loops but couldn't figure out how I can get this to work.
source to share
Using Java 8, you can simply createComparator
using the corresponding column as the key for the comparison, and then reverse that comparator. This will sort the array in place.
Arrays.sort(infoArray,
Comparator.comparing((String[] entry) -> Double.parseDouble(entry[2]))
.reversed());
Or for older Java versions, create your own comparator implementing compare
:
Arrays.sort(infoArray, new Comparator<String[]>() {
public int compare(String[] o1, String[] o2) {
return Double.compare(Double.parseDouble(o2[2]),
Double.parseDouble(o1[2]));
}
});
However, as noted in the comments, it might be wise to create class
for these information items and implement this class Comparable
:
class Info implements Comparable<Info> {
String nation;
String names;
double score;
// more stuff ...
public int compareTo(Info other) {
return Double.compare(this.score, other.score);
}
}
Then follow these steps:
Arrays.sort(infoArray, Comparator.reverseOrder());
source to share
You can create a class and you can set all fields. In your case 3 fields will be there den, or you can implement Comparable and in the compareTo method you can write your own logic that will depend on the numeric value and then you can sort it using Collections utility.
If you don't want to inject a compatible interface DLL, you can write your own Comparator class and pass it to the Collections utility method, which will sort according to your logic.
Please avoid using sets of sets if you have duplicates.
source to share
This is an XY Question because this is not the correct use of a 2D array in Java. When using a collection or n-dimensional array in Java, the data inside this data structure must always be of the same type (or at least within the inheritance hierarchy of that type).
As Alexis C said in his comment: Java is an object oriented language, so you should use class objects to store data. Then you can sort the structure in a specific field of that object.
Here's an example (I'm not entirely sure what the various components of the array are:
class MyClass {
private string countryCode;
private string names;
private double score;
public MyClass() {
countryCode = null;
names = null;
score = 0d;
}
public string getCountryCode() { return this.countryCode; }
public void setCountryCode(string code) { this.countryCode = code; }
//repeat the above pattern for names & score
}
Then you can add create object and add data to it as such:
MyClass data = new MyClass();
data.setCountryCode("GER");
data.setNames("Nathalie WEINZIERL Maylin & Daniel Peter LIEBERS Nelli & Alexander");
data.setScore(17.0);
This data can be added to a collection such as ArrayList and sorted.
List<MyClass> myDataCollection = new ArrayList<>();
myDataCollection.add(data);
//repeat for each object to populate the list
//sort the collection
Collections.sort(myDataCollection, Comparator.comparingDouble(s -> s.getScore()).reversed());
Be aware that this example is for Java 8. This allows us to use a lambda expression instead of creating an explicit comparator.
source to share