Reputation: 133
I want to rank some alternatives depending on their score and to print the name of each alternative with its ranking, how can I do this ?
Here is MWE :
import java.util.Arrays;
public class Ranking {
public static void main(String[] args) {
//The score of the alternatives :
double [] score = new double [4] ;
score[0] = 6.75 ;
score[1] = 9.0 ;
score[2] = 6.75 ;
score[3] = 5.50;
//The name of the alternatives :
String [] name = new String [4] ;
name[0] = "a1";
name[1] = "a2";
name[2] = "a3";
name[3] = "a4";
//Descending Sorting the score array like this :
for(int i=0;i<4;i++)
score[i]= - score[i];
Arrays.sort(score);
for(int i=0;i<4;i++)
score[i]= - score[i];
//print the result
for(int i=0;i<4;i++)
System.out.println(score[i] + " rank = " + (i+1));
}
//result :
//9.0 rank = 1
//6.75 rank = 2
//6.75 rank = 3
//5.5 rank = 4
But I want this kind of result :
name : a2 a1 a3 a4
rank : 1 2 3 4
How can I do this ?
Upvotes: 2
Views: 54
Reputation: 18235
You sort individual array so the result cannot be mapped between name - rank.
In java, the normal way is to abstract your data to object and operate on them:
Declare your model class:
public YourModel {
private String name;
private double score;
public Rank(String name, double score) {
this.name = name;
this.score = score;
}
// you create your getters/ setters
}
Add data to your array:
YourModel[] models = new YourModel[4];
models[0] = new YourModel("a1", 6.75D);
... add other models ...
Sort your arrays:
Arrays.sort(models, Comparator.comparing(YourModel::getScore));
After this step, you will have your array sorted:
[{"a2", 9.0} ....]
And you can print your result:
for(int i=0;i<4;i++)
System.out.println(models[i].getName() + " rank = " + (i+1));
}
Upvotes: 1
Reputation: 18245
When you have to sort something, you should look at TreeSet
or TreeMap
structures. Actually Array
is not where sorting is primary function.
It seems that you have unique names
for all objects, then you have to use TreeSet
with reverce comparator. As result when you iterate over the Set
, you get required order:
final class Model {
private final String name;
private final double score;
public Model(String name, double score) {
this.name = name;
this.score = score;
}
}
final Comparator<Model> reverceScore = (d1, d2) -> {
if (d1.name.equalsIgnoreCase(d2.name))
return 0;
int res = Double.compare(d2.score, d1.score);
return res != 0 ? res : d1.name.compareTo(d2.name);
};
Set<Model> data = new TreeSet<>(reverceScore);
data.add(new Model("a1", 6.75));
data.add(new Model("a2", 9));
data.add(new Model("a3", 6.75));
data.add(new Model("a4", 5.5));
int rank = 1;
for(Model model : data)
System.out.println("rank: " + rank++ + " - name: " + model.name + ", score: " + model.score);
Upvotes: 1
Reputation: 2751
You should use Map<String, Double>
for this.
Try below example:
Map<String, Double> map = new HashMap<>();
map.put("a1", 6.75);
map.put("a2", 9.0);
map.put("a3", 6.8);
map = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
//Iterate through the map
You will get the answer in reverse sorted order.
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
is used to sort by values and LinkedHashMap::new
is used to maintain ordering of Map
Upvotes: 1